Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(296)

Unified Diff: main.cc

Issue 4771007: AU: Timestamp logs at startup. (Closed) Base URL: ssh://git@gitrw.chromium.org:9222/update_engine.git@master
Patch Set: address review comments, switched mkdir perms Created 10 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | utils.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: main.cc
diff --git a/main.cc b/main.cc
index 571f33782905970c700a625a07ebe34cf1c68c40..0e5d3bc3085d8c02c7674fa76b885b6742b2b244 100644
--- a/main.cc
+++ b/main.cc
@@ -3,11 +3,11 @@
// found in the LICENSE file.
#include <string>
-#include <tr1/memory>
#include <vector>
#include <base/at_exit.h>
#include <base/command_line.h>
+#include <base/file_util.h>
#include <base/logging.h>
#include <base/string_util.h>
#include <gflags/gflags.h>
@@ -34,7 +34,6 @@ DEFINE_bool(foreground, false,
"Don't daemon()ize; run in foreground.");
using std::string;
-using std::tr1::shared_ptr;
using std::vector;
namespace chromeos_update_engine {
@@ -95,11 +94,59 @@ void SetupDbusService(UpdateEngineService* service) {
G_OBJECT(service));
}
-} // namespace {}
+void SetupLogSymlink(const string& symlink_path, const string& log_path) {
+ // TODO(petkov): To ensure a smooth transition between non-timestamped and
+ // timestamped logs, move an existing log to start the first timestamped
+ // one. This code can go away once all clients are switched to this version or
+ // we stop caring about the old-style logs.
+ if (utils::FileExists(symlink_path.c_str()) &&
+ !utils::IsSymlink(symlink_path.c_str())) {
+ file_util::ReplaceFile(FilePath(symlink_path), FilePath(log_path));
+ }
+ file_util::Delete(FilePath(symlink_path), true);
+ if (symlink(log_path.c_str(), symlink_path.c_str()) == -1) {
+ PLOG(ERROR) << "Unable to create symlink " << symlink_path
+ << " pointing at " << log_path;
+ }
+}
-} // namespace chromeos_update_engine
+string GetTimeAsString(time_t utime) {
+ struct tm tm;
+ CHECK(localtime_r(&utime, &tm) == &tm);
+ char str[16];
+ CHECK(strftime(str, sizeof(str), "%Y%m%d-%H%M%S", &tm) == 15);
+ return str;
+}
-#include "update_engine/subprocess.h"
+string SetupLogFile(const string& kLogsRoot) {
+ const string kLogSymlink = kLogsRoot + "/update_engine.log";
+ const string kLogsDir = kLogsRoot + "/update_engine";
+ const string kLogPath =
+ StringPrintf("%s/update_engine.%s",
+ kLogsDir.c_str(),
+ GetTimeAsString(::time(NULL)).c_str());
+ mkdir(kLogsDir.c_str(), 0755);
+ SetupLogSymlink(kLogSymlink, kLogPath);
+ return kLogSymlink;
+}
+
+void SetupLogging() {
+ // Log to stderr initially.
+ logging::InitLogging(NULL,
+ logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG,
+ logging::DONT_LOCK_LOG_FILE,
+ logging::APPEND_TO_OLD_LOG_FILE);
+ if (FLAGS_logtostderr) {
+ return;
+ }
+ const string log_file = SetupLogFile("/var/log");
+ logging::InitLogging(log_file.c_str(),
+ logging::LOG_ONLY_TO_FILE,
+ logging::DONT_LOCK_LOG_FILE,
+ logging::APPEND_TO_OLD_LOG_FILE);
+}
+} // namespace {}
+} // namespace chromeos_update_engine
int main(int argc, char** argv) {
::g_type_init();
@@ -110,12 +157,7 @@ int main(int argc, char** argv) {
chromeos_update_engine::Subprocess::Init();
google::ParseCommandLineFlags(&argc, &argv, true);
CommandLine::Init(argc, argv);
- logging::InitLogging("/var/log/update_engine.log",
- (FLAGS_logtostderr ?
- logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG :
- logging::LOG_ONLY_TO_FILE),
- logging::DONT_LOCK_LOG_FILE,
- logging::APPEND_TO_OLD_LOG_FILE);
+ chromeos_update_engine::SetupLogging();
if (!FLAGS_foreground)
PLOG_IF(FATAL, daemon(0, 0) == 1) << "daemon() failed";
« no previous file with comments | « no previous file | utils.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698