Skip to content

cvd: Add support for logging to a file#2506

Open
Databean wants to merge 1 commit intogoogle:mainfrom
Databean:log_files
Open

cvd: Add support for logging to a file#2506
Databean wants to merge 1 commit intogoogle:mainfrom
Databean:log_files

Conversation

@Databean
Copy link
Copy Markdown
Member

@Databean Databean commented May 2, 2026

Implement timestamped log file creation for the cvd command. Logs are saved in CvdDir() + "/logs/" with a name like
cvd_YYYYMMDD_HHMMSS.ms.log. This ensures logs are preserved across runs.

Assisted-by: Jetski jetski@google.com
Bug: b/507600500

@Databean Databean requested a review from jemoreira May 2, 2026 01:56
namespace cuttlefish {

std::vector<std::string> GetLogFiles() {
std::string log_dir = CvdDir() + "/logs";
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should use PerUserDir() instead.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

std::stringstream ss;
ss << std::put_time(&tm_now, "%Y%m%d_%H%M%S") << "." << std::setfill('0')
<< std::setw(3) << now_ms.count();
return {absl::StrCat(log_dir, "/cvd_", ss.str(), ".log")};
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why return a vector if it never returns more than one? Would an optional make more sense?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::optional makes more sense for the function, std::vector was more convenient to match the interface of LogToStderrAndFiles. Updated to return a std::optional.

Unfortunately adding an overload of LogToStderrAndFiles that takes a std::optional breaks several invocations that pass {} as the first argument because it becomes ambiguous between std::optional and std::vector even though the semantics of an empty optional and empty vector are the same.

#include <time.h>
#include <unistd.h>

#include <chrono>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

chrono ins't used in this file. Some of the other new headers may not be needed either.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, done.

@Databean Databean force-pushed the log_files branch 2 times, most recently from 6e45cfe to 41e0e3e Compare May 4, 2026 23:03
Implement timestamped log file creation for the cvd command. Logs are
saved in PerUserDir() + "/logs/" with a name like
cvd_YYYYMMDD_HHMMSS.ms.log. This ensures logs are preserved across runs.

Assisted-by: Jetski <jetski@google.com>
Bug: b/507600500
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants