1 // Copyright 2018 The Fuchsia Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 // 6 // This header contains definition for the logger object and protocol. 7 8 #ifndef LIB_SYSLOG_LOGGER_H_ 9 #define LIB_SYSLOG_LOGGER_H_ 10 11 #include <stdarg.h> 12 #include <unistd.h> 13 14 #include <zircon/types.h> 15 16 // Max no of tags associated with a logger. 17 #define FX_LOG_MAX_TAGS (4) 18 19 // Max individual tag length including terminating character. 20 #define FX_LOG_MAX_TAG_LEN (64) 21 22 // Log entry severity. 23 // Used for coarse filtering of log messages 24 typedef int fx_log_severity_t; 25 #define FX_LOG_INFO (0) 26 #define FX_LOG_WARNING (1) 27 #define FX_LOG_ERROR (2) 28 #define FX_LOG_FATAL (3) 29 30 __BEGIN_CDECLS 31 32 // Configuration for a logger object. 33 // Specifies the destination to which log messages should be written. 34 // Multiple destinations may be used concurrently. 35 typedef struct fx_logger_config { 36 // The minimum log severity. 37 // Log messages with lower severity will be discarded. 38 fx_log_severity_t min_severity; 39 40 // The file descriptor to which formatted log messages should be written, 41 // or -1 if log messages should not be written to the console. 42 // logger takes ownership of this fd. 43 int console_fd; 44 45 // The FIDL log service channel to which the logger should connect, or 46 // |ZX_HANDLE_INVALID| if the logger should not connect to the log service. 47 // logger takes ownership of this handle. 48 zx_handle_t log_service_channel; 49 50 // An array of tag strings to associate with all messages written 51 // by this logger. Tags will be truncated if they are (individually) longer 52 // than |FX_LOG_MAX_TAG_LEN|. 53 const char** tags; 54 55 // Number of tag strings. Must be no more than |FX_LOG_MAX_TAGS|. 56 size_t num_tags; 57 } fx_logger_config_t; 58 59 // Opaque type representing a logger object. 60 typedef struct fx_logger fx_logger_t; 61 62 // Creates a logger object from the specified configuration. 63 // 64 // This will return ZX_ERR_INVALID_ARGS if |num_tags| is more than 65 // |FX_LOG_MAX_TAGS| and return |ZX_ERR_INTERNAL} if dup fails. 66 // |config| can be safely deleted after this function returns. 67 zx_status_t fx_logger_create(const fx_logger_config_t* config, 68 fx_logger_t** out_logger); 69 70 // Destroys a logger object. 71 // 72 // This closes |console_fd| or |log_service_channel| which were passed in 73 // |fx_logger_config_t|. 74 void fx_logger_destroy(fx_logger_t* logger); 75 76 // Gets the logger's minimum log severity. 77 fx_log_severity_t fx_logger_get_min_severity(fx_logger_t* logger); 78 79 // Sets logger severity 80 void fx_logger_set_min_severity(fx_logger_t* logger, 81 fx_log_severity_t severity); 82 83 // Activates fallback mode and logger starts writing to |fallback_fd|. 84 // There is no way to revert this action. 85 // 86 // This function does not take ownership of |fallback_fd| and it should not be 87 // closed till this logger object is no longer in use. Logger will log to 88 // stderr if -1 is provided. 89 // 90 // This function is thread unsafe. 91 void fx_logger_activate_fallback(fx_logger_t* logger, 92 int fallback_fd); 93 94 // Writes formatted message to a logger. 95 // The message will be discarded if |severity| is less than the logger's 96 // minimum log severity. 97 // The |tag| may be NULL, in which case no additional tags are added to the 98 // log message. 99 // The |tag| will be truncated if it is longer than |FX_LOG_MAX_TAG_LEN|. 100 // No message is written if |message| is NULL. 101 zx_status_t fx_logger_logf(fx_logger_t* logger, fx_log_severity_t severity, 102 const char* tag, const char* msg, ...); 103 104 // Writes formatted message to a logger using varargs. 105 // The message will be discarded if |severity| is less than the logger's 106 // minimum log severity. 107 // The |tag| may be NULL, in which case no additional tags are added to the 108 // log message. 109 // The |tag| will be truncated if it is longer than |FX_LOG_MAX_TAG_LEN|. 110 // No message is written if |message| is NULL. 111 zx_status_t fx_logger_logvf(fx_logger_t* logger, fx_log_severity_t severity, 112 const char* tag, const char* msg, va_list args); 113 114 // Writes a message to a logger. 115 // The message will be discarded if |severity| is less than the logger's 116 // minimum log severity. 117 // The |tag| may be NULL, in which case no additional tags are added to the 118 // log message. 119 // The |tag| will be truncated if it is longer than |FX_LOG_MAX_TAG_LEN|. 120 // No message is written if |message| is NULL. 121 zx_status_t fx_logger_log(fx_logger_t* logger, fx_log_severity_t severity, 122 const char* tag, const char* msg); 123 124 __END_CDECLS 125 126 #endif // LIB_SYSLOG_LOGGER_H_ 127