RStudio Launcher Plugin SDK  1.1.3
A software development kit for creating plugins that work the the RStudio Launcher.
FileLogDestination.hpp
1 /*
2  * FileLogDestination.hpp
3  *
4  * Copyright (C) 2022 by RStudio, PBC
5  *
6  * Unless you have received this program directly from RStudio pursuant to the terms of a commercial license agreement
7  * with RStudio, then this program is licensed to you under the following terms:
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
10  * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
11  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18  * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  */
23 
24 #ifndef LAUNCHER_PLUGINS_FILE_LOG_DESTINATION_HPP
25 #define LAUNCHER_PLUGINS_FILE_LOG_DESTINATION_HPP
26 
27 #include <logging/ILogDestination.hpp>
28 
29 #include <string>
30 
31 #include <PImpl.hpp>
32 #include <system/FilePath.hpp>
33 
34 namespace rstudio {
35 namespace launcher_plugins {
36 namespace logging {
37 
42 {
43 public:
51  FileLogOptions(system::FilePath in_directory);
52 
61  FileLogOptions(system::FilePath in_directory,
62  bool in_warnSyslog);
63 
79  system::FilePath in_directory,
80  std::string in_fileMode,
81  double in_maxSizeMb,
82  int in_rotationDays,
83  int in_maxRotations,
84  int in_deletionDays,
85  bool in_doRotation,
86  bool in_includePid,
87  bool in_warnSyslog,
88  bool in_forceLogDirectory);
89 
95  int getDeletionDays() const;
96 
102  const system::FilePath& getDirectory() const;
103 
109  const std::string& getFileMode() const;
110 
116  bool getForceDirectory() const;
117 
123  int getMaxRotations() const;
124 
130  double getMaxSizeMb() const;
131 
137  int getRotationDays() const;
138 
144  bool doRotation() const;
145 
151  bool includePid() const;
152 
158  bool warnSyslog() const;
159 
165  void setDeletionDays(int in_deletionDays);
166 
173  void setDirectory(const system::FilePath& in_directory);
174 
181  void setFileMode(const std::string& in_fileMode);
182 
189  void setForceDirectory(bool in_forceDirectory);
190 
196  void setMaxRotations(int in_maxRotations);
197 
203  void setMaxSizeMb(double in_maxSizeMb);
204 
210  void setRotationDays(int in_rotationDays);
211 
217  void setDoRotation(bool in_doRotation);
218 
224  void setIncludePid(bool in_includePid);
225 
231  void setWarnSyslog(bool in_warnSyslog);
232 
233 private:
234  // Default values.
235  static constexpr const char* s_defaultFileMode = "600";
236  static constexpr int s_defaultMaxSizeMb = 2;
237  static constexpr int s_defaultRotationDays = 1;
238  static constexpr int s_defaultMaxRotations = 100;
239  static constexpr int s_defaultDeletionDays = 30;
240  static constexpr bool s_defaultDoRotation = true;
241  static constexpr bool s_defaultIncludePid = false;
242  static constexpr bool s_defaultWarnSyslog = true;
243  static constexpr bool s_defaultForceDirectory = false;
244 
245  // The directory where log files should be written.
246  system::FilePath m_directory;
247 
248  // The permissions to set on log files.
249  std::string m_fileMode;
250 
251  // The maximum size of log files, in MB.
252  double m_maxSizeMb;
253 
254  // The number of days a log file should persist before being rotated.
255  int m_rotationDays;
256 
257  // The maximum number of rotated log files that are allowed to exist.
258  int m_maxRotations;
259 
260  // The number of days a log file should persist before being deleted.
261  int m_deletionDays;
262 
263  // Whether to rotate log files or not.
264  bool m_doRotation;
265 
266  // Whether to include the PID in logs.
267  bool m_includePid;
268 
269  // Whether to also send warn/error logs to syslog for admin visibility.
270  bool m_warnSyslog;
271 
272  // Whether or not to force the directory to prevent user override.
273  bool m_forceDirectory;
274 };
275 
280 {
281 public:
298  const std::string& in_id,
299  LogLevel in_logLevel,
300  LogMessageFormatType in_formatType,
301  const std::string& in_programId,
302  FileLogOptions in_logOptions,
303  bool in_reloadable = false);
304 
308  ~FileLogDestination() override;
309 
313  std::string path();
314 
320  void refresh(const RefreshParams& in_refreshParams = RefreshParams()) override;
321 
329  void writeLog(LogLevel in_logLevel, const std::string& in_message) override;
330 
331 private:
332  PRIVATE_IMPL_SHARED(m_impl);
333 };
334 
335 } // namespace logging
336 } // namespace launcher_plugins
337 } // namespace rstudio
338 
339 #endif
rstudio::launcher_plugins::logging::FileLogOptions::setDirectory
void setDirectory(const system::FilePath &in_directory)
Sets the directory where log files should be written.
rstudio::launcher_plugins::system::FilePath
Class which represents a path on the system. May be any type of file (e.g. directory,...
Definition: FilePath.hpp:77
rstudio::launcher_plugins::logging::FileLogDestination::FileLogDestination
FileLogDestination(const std::string &in_id, LogLevel in_logLevel, LogMessageFormatType in_formatType, const std::string &in_programId, FileLogOptions in_logOptions, bool in_reloadable=false)
Constructor.
rstudio::launcher_plugins::logging::ILogDestination
Interface which allows a logger to write a log message to a destination.
Definition: ILogDestination.hpp:40
rstudio::launcher_plugins::logging::FileLogOptions::getDirectory
const system::FilePath & getDirectory() const
Gets the directory where log files should be written.
rstudio::launcher_plugins::logging::FileLogOptions::setWarnSyslog
void setWarnSyslog(bool in_warnSyslog)
Sets whether or not to also send warn/error logs to syslog for admin visibility.
rstudio::launcher_plugins::logging::FileLogOptions::getMaxRotations
int getMaxRotations() const
Gets the maximum number of allowed rotated log files.
rstudio::launcher_plugins::logging::FileLogOptions::setIncludePid
void setIncludePid(bool in_includePid)
Sets whether or not to include the PID of the process in the log filename.
rstudio::launcher_plugins::logging::FileLogOptions::getFileMode
const std::string & getFileMode() const
Gets the permissions with which log files should be created.
rstudio::launcher_plugins::logging::FileLogDestination::refresh
void refresh(const RefreshParams &in_refreshParams=RefreshParams()) override
Refreshes the log destintation. Ensures that the log does not have any stale file handles.
rstudio::launcher_plugins::logging::FileLogOptions
Class which represents the options for a file logger.
Definition: FileLogDestination.hpp:41
rstudio::launcher_plugins::logging::LogLevel
LogLevel
Enum which represents the level of detail at which to log messages.
Definition: Logger.hpp:86
rstudio::launcher_plugins::logging::FileLogOptions::setMaxRotations
void setMaxRotations(int in_maxRotations)
Sets the maximum number of allowed rotated log files.
rstudio::launcher_plugins::logging::FileLogOptions::setRotationDays
void setRotationDays(int in_rotationDays)
Sets the number of days a log file should persist before being rotated.
rstudio::launcher_plugins::logging::RefreshParams
A struct encapsulating various params to pass when refreshing log destinations. This carries data tha...
Definition: Logger.hpp:67
rstudio::launcher_plugins::logging::FileLogOptions::warnSyslog
bool warnSyslog() const
Returns whether or not to also send warn/error logs to syslog for admin visibility.
rstudio::launcher_plugins::logging::FileLogOptions::setDeletionDays
void setDeletionDays(int in_deletionDays)
Sets the number of days a rotated log file should persist before being deleted.
rstudio::launcher_plugins::logging::FileLogOptions::doRotation
bool doRotation() const
Returns whether or not to rotate log files before overwriting them.
rstudio::launcher_plugins::logging::FileLogOptions::setForceDirectory
void setForceDirectory(bool in_forceDirectory)
Sets whether or not the log directory is forced, preventing user override.
rstudio::launcher_plugins::logging::FileLogOptions::includePid
bool includePid() const
Returns whether or not to include the PID in the log filename.
rstudio::launcher_plugins::logging::FileLogOptions::getForceDirectory
bool getForceDirectory() const
Gets whether or not the log directory is forced, preventing user override.
rstudio::launcher_plugins::logging::FileLogOptions::setFileMode
void setFileMode(const std::string &in_fileMode)
Sets the permissions with which the log files should be created.
rstudio::launcher_plugins::logging::FileLogOptions::getDeletionDays
int getDeletionDays() const
Gets the number of days a rotated log file should persist before being deleted.
rstudio::launcher_plugins::logging::FileLogOptions::setMaxSizeMb
void setMaxSizeMb(double in_maxSizeMb)
Sets the maximum size of the log files, in MB.
rstudio::launcher_plugins::logging::FileLogOptions::setDoRotation
void setDoRotation(bool in_doRotation)
Sets whether or not to rotate log files before overwriting them.
rstudio::launcher_plugins::logging::LogMessageFormatType
LogMessageFormatType
Enum which represents the format type for log messages.
Definition: Logger.hpp:107
rstudio::launcher_plugins::logging::FileLogOptions::getRotationDays
int getRotationDays() const
Gets the number of days a log file should persist before being rotated.
rstudio::launcher_plugins::logging::FileLogDestination
Class which allows sending log messages to a file.
Definition: FileLogDestination.hpp:279
rstudio::launcher_plugins::logging::FileLogOptions::getMaxSizeMb
double getMaxSizeMb() const
Gets the maximum size of log files, in MB.
rstudio::launcher_plugins::logging::FileLogDestination::~FileLogDestination
~FileLogDestination() override
Destructor.
rstudio::launcher_plugins::logging::FileLogDestination::path
std::string path()
Returns the log destination.
rstudio::launcher_plugins::logging::FileLogDestination::writeLog
void writeLog(LogLevel in_logLevel, const std::string &in_message) override
Writes a message to the log file.
rstudio::launcher_plugins::logging::FileLogOptions::FileLogOptions
FileLogOptions(system::FilePath in_directory)
Constructor.