RStudio Launcher Plugin SDK  1.1.3
A software development kit for creating plugins that work the the RStudio Launcher.
Logger.hpp
Go to the documentation of this file.
1 /*
2  * Logger.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_LOGGER_HPP
25 #define LAUNCHER_PLUGINS_LOGGER_HPP
26 
27 #include <map>
28 #include <memory>
29 #include <ostream>
30 #include <string>
31 #include <vector>
32 
33 #include <boost/any.hpp>
34 #include <boost/function.hpp>
35 #include <Optional.hpp>
36 
37 #include <system/User.hpp>
38 
39 #include <PImpl.hpp>
40 
41 namespace rstudio {
42 namespace launcher_plugins {
43 
44 class Error;
45 class ErrorLocation;
46 
47 } // namespace rstudio
48 } // namespace launcher_plugins
49 
50 namespace rstudio {
51 namespace launcher_plugins {
52 namespace logging {
53 
59 class ILogDestination;
60 
68 {
74  bool chownLogDir;
75 };
76 
80 constexpr char s_delim = ';';
81 
86 enum class LogLevel
87 {
88  OFF = 0, // No messages will be logged.
89  ERR = 1, // Error messages will be logged.
90  WARN = 2, // Warning and error messages will be logged.
91  INFO = 3, // Info, warning, and error messages will be logged.
92  DEBUG = 4 // All messages will be logged.
93 };
94 
101 typedef std::vector<std::pair<std::string, boost::any>> LogMessageProperties;
102 
108 {
109  PRETTY = 0, // A human-readable single line log message format
110  JSON = 1 // A JSON format, one JSON object per line
111 };
112 
120 std::string cleanDelimiters(const std::string& in_str);
121 
127 void setProgramId(const std::string& in_programId);
128 
136 void addLogDestination(const std::shared_ptr<ILogDestination>& in_destination);
137 
149 void addLogDestination(const std::shared_ptr<ILogDestination>& in_destination, const std::string& in_section);
150 
156 bool hasFileLogDestination();
157 
163 bool hasStderrLogDestination();
164 
170 bool isLogLevel(logging::LogLevel level);
171 
179 std::string cleanDelims(const std::string& in_toClean);
180 
189 void logError(const Error& in_error);
190 
201 void logError(const Error& in_error, const ErrorLocation& in_location);
202 
211 void logErrorAsWarning(const Error& in_error);
212 
221 void logErrorAsInfo(const Error& in_error);
222 
231 void logErrorAsDebug(const Error& in_error);
232 
242 void logErrorMessage(const std::string& in_message, const std::string& in_section = std::string());
243 
253 void logErrorMessage(const std::string& in_message, const ErrorLocation& in_loggedFrom);
254 
266 void logErrorMessage(const std::string& in_message,
267  const std::string& in_section,
268  const Optional<LogMessageProperties>& in_properties,
269  const ErrorLocation& in_loggedFrom);
270 
280 void logWarningMessage(const std::string& in_message, const std::string& in_section = std::string());
281 
291 void logWarningMessage(const std::string& in_message, const ErrorLocation& in_loggedFrom);
292 
304 void logWarningMessage(const std::string& in_message,
305  const std::string& in_section,
306  const Optional<LogMessageProperties>& in_properties,
307  const ErrorLocation& in_loggedFrom);
308 
318 void logDebugMessage(const std::string& in_message, const std::string& in_section = std::string());
319 
329 void logDebugMessage(const std::string& in_message, const ErrorLocation& in_loggedFrom);
330 
342 void logDebugMessage(const std::string& in_message,
343  const std::string& in_section,
344  const Optional<LogMessageProperties>& in_properties,
345  const ErrorLocation& in_loggedFrom);
346 
356 void logDebugAction(const boost::function<std::string(Optional<LogMessageProperties>*)>& in_action,
357  const std::string& in_section = std::string());
358 
368 void logInfoMessage(const std::string& in_message, const std::string& in_section = std::string());
369 
379 void logInfoMessage(const std::string& in_message, const ErrorLocation& in_loggedFrom);
380 
392 void logInfoMessage(const std::string& in_message,
393  const std::string& in_section,
394  const Optional<LogMessageProperties>& in_properties,
395  const ErrorLocation& in_loggedFrom);
396 
405 void logPassthroughMessage(const std::string& in_source, const std::string& in_message);
406 
412 void refreshAllLogDestinations(const logging::RefreshParams& in_refreshParams = logging::RefreshParams());
413 
424 void removeLogDestination(const std::string& in_destinationId, const std::string& in_section = std::string());
425 
430 void removeReloadableLogDestinations();
431 
440  std::ostream& writeError(const Error& in_error, std::ostream& io_os);
448 std::string writeError(const Error& in_error);
449 
450 } // namespace logging
451 } // namespace launcher_plugins
452 } // namespace rstudio
453 
454 #endif
rstudio::launcher_plugins::ErrorLocation
Class which represents the location of an error.
Definition: Error.hpp:74
rstudio::launcher_plugins::logging::LogMessageProperties
std::vector< std::pair< std::string, boost::any > > LogMessageProperties
Convenience type for log message properties. Using a boost::any instead of a boost::variant allows ac...
Definition: Logger.hpp:101
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::s_delim
constexpr char s_delim
Log delimiting character which may be used for custom log formatting.
Definition: Logger.hpp:80
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::Error
Class which represents an error.
Definition: Error.hpp:174
rstudio::launcher_plugins::logging::RefreshParams::newUser
Optional< launcher_plugins::system::User > newUser
An optional user to become the new owner of any open file logs. Used when changing process running us...
Definition: Logger.hpp:73
rstudio::launcher_plugins::Optional
Container class which represents a value that may or may not be set.
Definition: Optional.hpp:38
rstudio::launcher_plugins::logging::LogMessageFormatType
LogMessageFormatType
Enum which represents the format type for log messages.
Definition: Logger.hpp:107