1 /** @file
2 
3   A brief file description
4 
5   @section license License
6 
7   Licensed to the Apache Software Foundation (ASF) under one
8   or more contributor license agreements.  See the NOTICE file
9   distributed with this work for additional information
10   regarding copyright ownership.  The ASF licenses this file
11   to you under the Apache License, Version 2.0 (the
12   "License"); you may not use this file except in compliance
13   with the License.  You may obtain a copy of the License at
14 
15       http://www.apache.org/licenses/LICENSE-2.0
16 
17   Unless required by applicable law or agreed to in writing, software
18   distributed under the License is distributed on an "AS IS" BASIS,
19   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   See the License for the specific language governing permissions and
21   limitations under the License.
22  */
23 
24 #pragma once
25 
26 #include <string_view>
27 #include <string>
28 
29 #include "tscore/ink_platform.h"
30 #include "records/P_RecProcess.h"
31 #include "ProxyConfig.h"
32 #include "LogObject.h"
33 #include "RolledLogDeleter.h"
34 #include "tscpp/util/MemSpan.h"
35 
36 /* Instead of enumerating the stats in DynamicStats.h, each module needs
37    to enumerate its stats separately and register them with librecords
38    */
39 enum {
40   // Logging Events
41   log_stat_event_log_error_ok_stat,
42   log_stat_event_log_error_skip_stat,
43   log_stat_event_log_error_aggr_stat,
44   log_stat_event_log_error_full_stat,
45   log_stat_event_log_error_fail_stat,
46 
47   log_stat_event_log_access_ok_stat,
48   log_stat_event_log_access_skip_stat,
49   log_stat_event_log_access_aggr_stat,
50   log_stat_event_log_access_full_stat,
51   log_stat_event_log_access_fail_stat,
52 
53   // Logging Data
54   log_stat_num_sent_to_network_stat,
55   log_stat_num_lost_before_sent_to_network_stat,
56   log_stat_num_received_from_network_stat,
57   log_stat_num_flush_to_disk_stat,
58   log_stat_num_lost_before_flush_to_disk_stat,
59 
60   log_stat_bytes_lost_before_preproc_stat,
61   log_stat_bytes_sent_to_network_stat,
62   log_stat_bytes_lost_before_sent_to_network_stat,
63   log_stat_bytes_received_from_network_stat,
64 
65   log_stat_bytes_flush_to_disk_stat,
66   log_stat_bytes_lost_before_flush_to_disk_stat,
67   log_stat_bytes_written_to_disk_stat,
68   log_stat_bytes_lost_before_written_to_disk_stat,
69 
70   // Logging I/O
71   log_stat_log_files_open_stat,
72   log_stat_log_files_space_used_stat,
73 
74   log_stat_count
75 };
76 
77 extern RecRawStatBlock *log_rsb;
78 
79 struct dirent;
80 
81 /*-------------------------------------------------------------------------
82   this object keeps the state of the logging configuration variables.  upon
83   construction, the log configuration file is read and the logging
84   variables are initialized.
85 
86   the "global" logconfig object is kept as a static pointer in the log
87   class, called "config", and changed whenever the configuration variables
88   are changed in the config file, using log::change_configuration().
89 
90   to add a new config variable:
91      1. add a line in records.config for the new config variable.
92         the name in records.config should be "proxy.config.log.xxx".
93      2. create a member variable to store the current value.
94         the name of the member variable should be "xxx".
95      3. if the member variable is a string, add a delete for it in the
96         destructor, logconfig::~logconfig.
97      4. initialize the member variable in logconfig::setup_default_values
98      5. update the member variable from the records.config file
99         in logconfig::read_configuration_variables() using a call to
100         configreadinteger or configreadstring.
101      6. add a line in the logconfig::register_config_callbacks() function
102         for this new variable if it is exposed in the GUI
103   -------------------------------------------------------------------------*/
104 
105 class LogConfig : public ConfigInfo
106 {
107 public:
108   LogConfig();
109   ~LogConfig() override;
110 
111   void init(LogConfig *previous_config = nullptr);
112   void display(FILE *fd = stdout);
113   void setup_log_objects();
114 
115   static int reconfigure(const char *name, RecDataT data_type, RecData data, void *cookie);
116 
117   static void register_config_callbacks();
118   static void register_stat_callbacks();
119   static void register_mgmt_callbacks();
120 
121   bool space_to_write(int64_t bytes_to_write) const;
122 
123   bool
space_is_short()124   space_is_short() const
125   {
126     return !space_to_write(max_space_mb_headroom * LOG_MEGABYTE);
127   };
128 
129   void
increment_space_used(int bytes)130   increment_space_used(int bytes)
131   {
132     m_space_used += bytes;
133     m_partition_space_left -= bytes;
134   }
135 
136   void update_space_used();
137   void read_configuration_variables();
138 
139   // CVR This is the mgmt callback function, hence all the strange arguments
140   static void reconfigure_mgmt_variables(ts::MemSpan<void>);
141 
142   int
get_max_space_mb()143   get_max_space_mb() const
144   {
145     return max_space_mb_for_logs;
146   }
147 
148   void
transfer_objects(LogConfig * old_config)149   transfer_objects(LogConfig *old_config)
150   {
151     log_object_manager.transfer_objects(old_config->log_object_manager);
152   }
153 
154   bool
has_api_objects()155   has_api_objects() const
156   {
157     return log_object_manager.has_api_objects();
158   }
159 
160   /** Register rolled logs of logname for auto-deletion when there are space
161    * constraints.
162    *
163    * @param[in] logname The name of the unrolled log to register, such as
164    * "diags.log".
165    *
166    * @param[in] rolling_min_count The minimum amount of rolled logs of logname
167    * to try to keep around. A value of 0 expresses a desire to keep all rolled
168    * files, if possible.
169    */
170   void register_rolled_log_auto_delete(std::string_view logname, int rolling_min_count);
171 
172 public:
173   bool initialized             = false;
174   bool reconfiguration_needed  = false;
175   bool logging_space_exhausted = false;
176   int64_t m_space_used         = 0;
177   int64_t m_partition_space_left;
178   bool roll_log_files_now; // signal that files must be rolled
179 
180   LogObjectManager log_object_manager;
181 
182   LogFilterList filter_list;
183   LogFormatList format_list;
184 
185   int log_buffer_size;
186   int max_secs_per_buffer;
187   int max_space_mb_for_logs;
188   int max_space_mb_headroom;
189   int logfile_perm;
190 
191   int preproc_threads;
192 
193   Log::RollingEnabledValues rolling_enabled;
194   int rolling_interval_sec;
195   int rolling_offset_hr;
196   int rolling_size_mb;
197   int rolling_min_count;
198   int rolling_max_count;
199   bool rolling_allow_empty;
200   bool auto_delete_rolled_files;
201 
202   int sampling_frequency;
203   int file_stat_frequency;
204   int space_used_frequency;
205 
206   int ascii_buffer_size;
207   int max_line_size;
208   int logbuffer_max_iobuf_index;
209 
210   char *hostname;
211   char *logfile_dir;
212 
213 private:
214   bool evaluate_config();
215 
216   void setup_default_values();
217 
218 private:
219   bool m_disk_full                  = false;
220   bool m_disk_low                   = false;
221   bool m_partition_full             = false;
222   bool m_partition_low              = false;
223   bool m_log_directory_inaccessible = false;
224 
225   RolledLogDeleter rolledLogDeleter;
226 
227   // noncopyable
228   // -- member functions not allowed --
229   LogConfig(const LogConfig &) = delete;
230   LogConfig &operator=(const LogConfig &) = delete;
231 };
232