Apollo  6.0
Open source self driving car software
log_file_object.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2018 The Apollo Authors. All Rights Reserved.
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 #ifndef CYBER_LOGGER_LOG_FILE_OBJECT_H_
18 #define CYBER_LOGGER_LOG_FILE_OBJECT_H_
19 
20 #include <cstdint>
21 #include <iomanip>
22 #include <mutex>
23 #include <string>
24 
25 #include "glog/logging.h"
26 
27 namespace apollo {
28 namespace cyber {
29 namespace logger {
30 
31 // the C99 format
32 typedef int32_t int32;
33 typedef uint32_t uint32;
34 typedef int64_t int64;
35 typedef uint64_t uint64;
36 
37 using google::LogSeverity;
38 using google::NUM_SEVERITIES;
39 using std::ostringstream;
40 using std::setw;
41 using std::string;
42 
43 // Encapsulates all file-system related state
44 class LogFileObject : public google::base::Logger {
45  public:
46  LogFileObject(LogSeverity severity, const char* base_filename);
48 
49  void Write(bool force_flush, // Should we force a flush here?
50  time_t timestamp, // Timestamp for this entry
51  const char* message, int message_len) override;
52 
53  // Configuration options
54  void SetBasename(const char* basename);
55  void SetExtension(const char* ext);
56  void SetSymlinkBasename(const char* symlink_basename);
57 
58  // Normal flushing routine
59  void Flush() override;
60 
61  // It is the actual file length for the system loggers,
62  // i.e., INFO, ERROR, etc.
63  uint32 LogSize() override {
64  std::lock_guard<std::mutex> lock(lock_);
65  return file_length_;
66  }
67 
68  // Internal flush routine. Exposed so that FlushLogFilesUnsafe()
69  // can avoid grabbing a lock. Usually Flush() calls it after
70  // acquiring lock_.
71  void FlushUnlocked();
72 
73  const string& hostname();
74 
75  private:
76  static const uint32 kRolloverAttemptFrequency = 0x20;
77 
78  std::mutex lock_;
79  bool base_filename_selected_;
80  string base_filename_;
81  string symlink_basename_;
82  string filename_extension_; // option users can specify (eg to add port#)
83  FILE* file_;
84  LogSeverity severity_;
85  uint32 bytes_since_flush_;
86  uint32 file_length_;
87  unsigned int rollover_attempt_;
88  int64 next_flush_time_; // cycle count at which to flush log
89  string hostname_;
90 
91  // Actually create a logfile using the value of base_filename_ and the
92  // supplied argument time_pid_string
93  // REQUIRES: lock_ is held
94  bool CreateLogfile(const string& time_pid_string);
95 };
96 
97 } // namespace logger
98 } // namespace cyber
99 } // namespace apollo
100 
101 #endif // CYBER_LOGGER_LOG_FILE_OBJECT_H_
int32_t int32
Definition: log_file_object.h:32
Definition: log_file_object.h:44
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
void SetSymlinkBasename(const char *symlink_basename)
int64_t int64
Definition: log_file_object.h:34
void Write(bool force_flush, time_t timestamp, const char *message, int message_len) override
void SetExtension(const char *ext)
uint64_t uint64
Definition: log_file_object.h:35
uint32 LogSize() override
Definition: log_file_object.h:63
void SetBasename(const char *basename)
uint32_t uint32
Definition: log_file_object.h:33
LogFileObject(LogSeverity severity, const char *base_filename)