• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef WEBSERVER_WEBSERVD_LOG_MANAGER_H_
16 #define WEBSERVER_WEBSERVD_LOG_MANAGER_H_
17 
18 #include <memory>
19 #include <string>
20 
21 #include <base/files/file_path.h>
22 #include <base/macros.h>
23 #include <base/memory/weak_ptr.h>
24 #include <base/time/time.h>
25 
26 struct sockaddr;
27 
28 namespace webservd {
29 
30 // A class that manages web server log files and helps with logging web request
31 // information.
32 class LogManager final {
33  public:
34   // Abstract interface for writing a log entry to a storage medium.
35   // LogManager provides its own implementation for writing to a log file,
36   // while tests can do something different.
37   struct LoggerInterface {
38     virtual ~LoggerInterface() = default;
39     virtual void Log(const base::Time& timestamp, const std::string& entry) = 0;
40   };
41 
42   LogManager() = default;
43 
44   // Initializes the logger and sets the log output directory.
45   static void Init(const base::FilePath& log_directory);
46 
47   // Called when a request completes, so a new log entry can be added to log.
48   static void OnRequestCompleted(const base::Time& timestamp,
49                                  const sockaddr* client_addr,
50                                  const std::string& method,
51                                  const std::string& url,
52                                  const std::string& version,
53                                  int status_code,
54                                  int64_t response_size);
55 
56   // Set a custom logger interface to do stuff other than log to a file.
57   static void SetLogger(std::unique_ptr<LoggerInterface> logger);
58 
59  private:
60   friend class FileLogger;
61 
62   // Returns the singleton instance of this class.
63   static LogManager* GetInstance();
64 
65   // Keeps the last several days' worth of logs and purges the rest, to make
66   // sure the log size is kept at bay.
67   void PerformLogMaintenance();
68 
69   // Directory to write the logs to.
70   base::FilePath log_directory_;
71 
72   // Logger interface (can be replaced for testing).
73   std::unique_ptr<LoggerInterface> logger_;
74 
75   base::WeakPtrFactory<LogManager> weak_ptr_factory_{this};
76   DISALLOW_COPY_AND_ASSIGN(LogManager);
77 };
78 
79 }  // namespace webservd
80 
81 #endif  // WEBSERVER_WEBSERVD_LOG_MANAGER_H_
82