1 //
2 // Copyright (C) 2020 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include <sys/stat.h>
18 #include <sys/types.h>
19 #include <unistd.h>
20
21 #include <string>
22
23 #include <base/files/file_util.h>
24 #include <base/logging.h>
25 #include <base/strings/string_util.h>
26 #include <base/strings/stringprintf.h>
27
28 #include "update_engine/common/utils.h"
29 #include "update_engine/logging.h"
30
31 using std::string;
32
33 namespace chromeos_update_engine {
34
35 namespace {
36
37 constexpr char kSystemLogsRoot[] = "/var/log";
38
SetupLogSymlink(const string & symlink_path,const string & log_path)39 void SetupLogSymlink(const string& symlink_path, const string& log_path) {
40 // TODO(petkov): To ensure a smooth transition between non-timestamped and
41 // timestamped logs, move an existing log to start the first timestamped
42 // one. This code can go away once all clients are switched to this version or
43 // we stop caring about the old-style logs.
44 if (utils::FileExists(symlink_path.c_str()) &&
45 !utils::IsSymlink(symlink_path.c_str())) {
46 base::ReplaceFile(
47 base::FilePath(symlink_path), base::FilePath(log_path), nullptr);
48 }
49 base::DeleteFile(base::FilePath(symlink_path), true);
50 if (symlink(log_path.c_str(), symlink_path.c_str()) == -1) {
51 PLOG(ERROR) << "Unable to create symlink " << symlink_path
52 << " pointing at " << log_path;
53 }
54 }
55
SetupLogFile(const string & kLogsRoot)56 string SetupLogFile(const string& kLogsRoot) {
57 const string kLogSymlink = kLogsRoot + "/update_engine.log";
58 const string kLogsDir = kLogsRoot + "/update_engine";
59 const string kLogPath =
60 base::StringPrintf("%s/update_engine.%s",
61 kLogsDir.c_str(),
62 utils::GetTimeAsString(::time(nullptr)).c_str());
63 mkdir(kLogsDir.c_str(), 0755);
64 SetupLogSymlink(kLogSymlink, kLogPath);
65 return kLogSymlink;
66 }
67
68 } // namespace
69
SetupLogging(bool log_to_system,bool log_to_file)70 void SetupLogging(bool log_to_system, bool log_to_file) {
71 logging::LoggingSettings log_settings;
72 log_settings.lock_log = logging::DONT_LOCK_LOG_FILE;
73 log_settings.logging_dest = static_cast<logging::LoggingDestination>(
74 (log_to_system ? logging::LOG_TO_SYSTEM_DEBUG_LOG : 0) |
75 (log_to_file ? logging::LOG_TO_FILE : 0));
76 log_settings.log_file = nullptr;
77
78 string log_file;
79 if (log_to_file) {
80 log_file = SetupLogFile(kSystemLogsRoot);
81 log_settings.delete_old = logging::APPEND_TO_OLD_LOG_FILE;
82 log_settings.log_file = log_file.c_str();
83 }
84 logging::InitLogging(log_settings);
85 }
86
87 } // namespace chromeos_update_engine
88