blob: 8b6c556b2a75172a8d37cea440f655971ea2ca2c [file] [log] [blame]
Yifan Hongc80de2d2020-02-25 17:13:53 -08001//
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
Amin Hassaniec7bc112020-10-29 16:47:58 -070028#include "update_engine/common/logging.h"
Yifan Hongc80de2d2020-02-25 17:13:53 -080029#include "update_engine/common/utils.h"
Yifan Hongc80de2d2020-02-25 17:13:53 -080030
31using std::string;
32
33namespace chromeos_update_engine {
34
35namespace {
36
37constexpr char kSystemLogsRoot[] = "/var/log";
38
39void 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 }
hscham043355b2020-11-17 16:50:10 +090049 base::DeletePathRecursively(base::FilePath(symlink_path));
Yifan Hongc80de2d2020-02-25 17:13:53 -080050 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
56string 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
70void 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;
Qijiang Fand55ec442020-09-28 15:11:40 +090082#if BASE_VER < 780000
Yifan Hongc80de2d2020-02-25 17:13:53 -080083 log_settings.log_file = log_file.c_str();
Qijiang Fand55ec442020-09-28 15:11:40 +090084#else
85 log_settings.log_file_path = log_file.c_str();
86#endif
Yifan Hongc80de2d2020-02-25 17:13:53 -080087 }
88 logging::InitLogging(log_settings);
89}
90
91} // namespace chromeos_update_engine