main.cc revision 5bac949c8339cd271a11b5b3bc5e16f7e8a0f2f
15bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa// Copyright 2014 The Chromium OS Authors. All rights reserved. 25bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa// Use of this source code is governed by a BSD-style license that can be 35bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa// found in the LICENSE file. 45bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa 55bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa#include <string> 65bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa 75bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa#include <base/at_exit.h> 85bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa#include <base/command_line.h> 95bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa#include <base/file_util.h> 105bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa#include <base/files/file_path.h> 115bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa#include <base/logging.h> 125bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa#include <base/message_loop/message_loop.h> 135bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa#include <base/strings/string_util.h> 145bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa#include <base/strings/stringprintf.h> 155bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa#include <gflags/gflags.h> 165bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa 175bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa#include "buffet/dbus_manager.h" 185bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa 195bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris SosaDEFINE_string(logsroot, "/var/log", "Root directory for buffet logs."); 205bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa 215bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosanamespace { 225bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa 235bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa// Returns |utime| as a string 245bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosastd::string GetTimeAsString(time_t utime) { 255bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa struct tm tm; 265bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa CHECK_EQ(localtime_r(&utime, &tm), &tm); 275bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa char str[16]; 285bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa CHECK_EQ(strftime(str, sizeof(str), "%Y%m%d-%H%M%S", &tm), 15UL); 295bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa return std::string(str); 305bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa} 315bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa 325bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa// Sets up a symlink to point to log file. 335bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosavoid SetupLogSymlink(const std::string& symlink_path, 345bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa const std::string& log_path) { 355bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa base::DeleteFile(base::FilePath(symlink_path), true); 365bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa if (symlink(log_path.c_str(), symlink_path.c_str()) == -1) { 375bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa LOG(ERROR) << "Unable to create symlink " << symlink_path 385bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa << " pointing at " << log_path; 395bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa } 405bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa} 415bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa 425bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa// Creates new log file based on timestamp in |logs_root|/buffet. 435bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosastd::string SetupLogFile(const std::string& logs_root) { 445bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa const auto log_symlink = logs_root + "/buffet.log"; 455bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa const auto logs_dir = logs_root + "/buffet"; 465bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa const auto log_path = 475bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa base::StringPrintf("%s/buffet.%s", 485bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa logs_dir.c_str(), 495bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa GetTimeAsString(::time(NULL)).c_str()); 505bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa mkdir(logs_dir.c_str(), 0755); 515bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa SetupLogSymlink(log_symlink, log_path); 525bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa return log_symlink; 535bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa} 545bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa 555bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa// Sets up logging for buffet. 565bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosavoid SetupLogging(const std::string& logs_root) { 575bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa const auto log_file = SetupLogFile(logs_root); 585bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa logging::LoggingSettings settings; 595bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa settings.logging_dest = logging::LOG_TO_ALL; 605bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa settings.log_file = log_file.c_str(); 615bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa settings.lock_log = logging::DONT_LOCK_LOG_FILE; 625bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa settings.delete_old = logging::APPEND_TO_OLD_LOG_FILE; 635bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa logging::InitLogging(settings); 645bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa} 655bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa 665bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa} // namespace 675bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa 685bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosaint main(int argc, char* argv[]) { 695bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa // Parse the args and check for extra args. 705bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa CommandLine::Init(argc, argv); 715bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa google::ParseCommandLineFlags(&argc, &argv, true); 725bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa CHECK_EQ(argc, 1) << "Unexpected arguments. Try --help"; 735bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa 745bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa SetupLogging(FLAGS_logsroot); 755bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa 765bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa base::AtExitManager at_exit_manager; 775bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa base::MessageLoopForIO message_loop; 785bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa 795bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa // Initialize the dbus_manager. 805bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa buffet::DBusManager dbus_manager; 815bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa dbus_manager.Init(); 825bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa 835bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa message_loop.Run(); 845bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa 855bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa dbus_manager.Finalize(); 865bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa return 0; 875bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa} 88