main.cc revision 4b5f04c56a2f13b4ec178ce430ea57ddadf1225f
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" 184b5f04c56a2f13b4ec178ce430ea57ddadf1225fChristopher Wiley#include "buffet/manager.h" 195bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa 205bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris SosaDEFINE_string(logsroot, "/var/log", "Root directory for buffet logs."); 215bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa 225bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosanamespace { 235bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa 245bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa// Returns |utime| as a string 255bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosastd::string GetTimeAsString(time_t utime) { 265bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa struct tm tm; 275bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa CHECK_EQ(localtime_r(&utime, &tm), &tm); 285bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa char str[16]; 295bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa CHECK_EQ(strftime(str, sizeof(str), "%Y%m%d-%H%M%S", &tm), 15UL); 305bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa return std::string(str); 315bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa} 325bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa 335bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa// Sets up a symlink to point to log file. 345bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosavoid SetupLogSymlink(const std::string& symlink_path, 355bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa const std::string& log_path) { 365bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa base::DeleteFile(base::FilePath(symlink_path), true); 375bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa if (symlink(log_path.c_str(), symlink_path.c_str()) == -1) { 385bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa LOG(ERROR) << "Unable to create symlink " << symlink_path 395bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa << " pointing at " << log_path; 405bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa } 415bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa} 425bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa 435bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa// Creates new log file based on timestamp in |logs_root|/buffet. 445bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosastd::string SetupLogFile(const std::string& logs_root) { 455bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa const auto log_symlink = logs_root + "/buffet.log"; 465bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa const auto logs_dir = logs_root + "/buffet"; 475bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa const auto log_path = 485bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa base::StringPrintf("%s/buffet.%s", 495bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa logs_dir.c_str(), 505bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa GetTimeAsString(::time(NULL)).c_str()); 515bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa mkdir(logs_dir.c_str(), 0755); 525bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa SetupLogSymlink(log_symlink, log_path); 535bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa return log_symlink; 545bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa} 555bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa 565bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa// Sets up logging for buffet. 575bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosavoid SetupLogging(const std::string& logs_root) { 585bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa const auto log_file = SetupLogFile(logs_root); 595bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa logging::LoggingSettings settings; 605bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa settings.logging_dest = logging::LOG_TO_ALL; 615bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa settings.log_file = log_file.c_str(); 625bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa settings.lock_log = logging::DONT_LOCK_LOG_FILE; 635bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa settings.delete_old = logging::APPEND_TO_OLD_LOG_FILE; 645bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa logging::InitLogging(settings); 655bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa} 665bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa 675bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa} // namespace 685bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa 695bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosaint main(int argc, char* argv[]) { 705bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa // Parse the args and check for extra args. 715bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa CommandLine::Init(argc, argv); 725bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa google::ParseCommandLineFlags(&argc, &argv, true); 735bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa CHECK_EQ(argc, 1) << "Unexpected arguments. Try --help"; 745bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa 755bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa SetupLogging(FLAGS_logsroot); 765bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa 775bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa base::AtExitManager at_exit_manager; 785bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa base::MessageLoopForIO message_loop; 795bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa 805bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa // Initialize the dbus_manager. 815bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa buffet::DBusManager dbus_manager; 825bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa dbus_manager.Init(); 834b5f04c56a2f13b4ec178ce430ea57ddadf1225fChristopher Wiley { 844b5f04c56a2f13b4ec178ce430ea57ddadf1225fChristopher Wiley // The Manager needs the dbus_manager to remain in scope for its lifetime. 854b5f04c56a2f13b4ec178ce430ea57ddadf1225fChristopher Wiley buffet::Manager manager(&dbus_manager); 864b5f04c56a2f13b4ec178ce430ea57ddadf1225fChristopher Wiley message_loop.Run(); 874b5f04c56a2f13b4ec178ce430ea57ddadf1225fChristopher Wiley } 885bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa 895bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa dbus_manager.Finalize(); 905bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa return 0; 915bac949c8339cd271a11b5b3bc5e16f7e8a0f2fChris Sosa} 92