LiteCore
Couchbase Lite cross-platform core implementation
Loading...
Searching...
No Matches
c4Log.h
Go to the documentation of this file.
1//
2// c4Log.h
3//
4// Copyright 2021-Present Couchbase, Inc.
5//
6// Use of this software is governed by the Business Source License included
7// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
8// in that file, in accordance with the Business Source License, use of this
9// software will be governed by the Apache License, Version 2.0, included in
10// the file licenses/APL2.txt.
11//
12
13#pragma once
14#include "c4Base.h"
15#include "c4Error.h"
16#include "fleece/FLSlice.h"
17#include <stdarg.h>
18
21
22
25
26/*
27 * NOTE: All logging functions are thread-safe
28 */
29
39
41typedef struct c4LogDomain* C4LogDomain;
42
43
51
61
69
75
78typedef void (*C4LogObserverCallback)(const C4LogEntry*, void* C4NULLABLE context);
79
91
92#pragma mark - LOG OBSERVERS:
93
94
99
105 C4Error* C4NULLABLE outError) C4API;
106
111
122
126
130
131
132#pragma mark - LOG DOMAINS:
133
134
139CBL_CORE_API C4LogDomain c4log_getDomain(const char* name, bool create) C4API;
140
143
148
151
159
167
168
169#pragma mark - LOGGING EXCEPTIONs:
170
171
176
179
183
184
185#pragma mark - WRITING LOG MESSAGES:
186
187
195CBL_CORE_API void c4log(C4LogDomain domain, C4LogLevel level, const char* fmt, ...) C4API __printflike(3, 4);
196
198CBL_CORE_API void c4vlog(C4LogDomain domain, C4LogLevel level, const char* fmt, va_list args) C4API __printflike(3, 0);
199
202
203// Convenient aliases for c4log:
204#define C4LogToAt(DOMAIN, LEVEL, FMT, ...) \
205 do { \
206 if ( c4log_willLog(DOMAIN, LEVEL) ) c4log(DOMAIN, LEVEL, FMT, ##__VA_ARGS__); \
207 } while ( false )
208#define C4Debug(FMT, ...) C4LogToAt(kC4DefaultLog, kC4LogDebug, FMT, ##__VA_ARGS__)
209#define C4Log(FMT, ...) C4LogToAt(kC4DefaultLog, kC4LogInfo, FMT, ##__VA_ARGS__)
210#define C4LogVerbose(FMT, ...) C4LogToAt(kC4DefaultLog, kC4LogVerbose, FMT, ##__VA_ARGS__)
211#define C4Warn(FMT, ...) C4LogToAt(kC4DefaultLog, kC4LogWarning, FMT, ##__VA_ARGS__)
212#define C4WarnError(FMT, ...) C4LogToAt(kC4DefaultLog, kC4LogError, FMT, ##__VA_ARGS__)
213
214
215#pragma mark - LEGACY LOG FILE/CALLBACK API:
216
217
225
228
231
235
238
240typedef void (*C4NULLABLE C4LogCallback)(C4LogDomain, C4LogLevel, const char* fmt, va_list args);
241
250CBL_CORE_API void c4log_writeToCallback(C4LogLevel level, C4LogCallback callback, bool preformatted) C4API;
251
254
258
261
263
264
#define NODISCARD
Definition CompilerSupport.h:63
#define CBL_CORE_API
Definition c4Compat.h:131
#define C4NULLABLE
Definition c4Compat.h:38
#define C4_ENUM(_type, _name)
Definition c4Compat.h:62
#define C4API
Definition c4Compat.h:106
#define C4_ASSUME_NONNULL_BEGIN
Definition c4Compat.h:36
#define C4API_END_DECLS
Definition c4Compat.h:108
#define __printflike(fmtarg, firstvararg)
Definition c4Compat.h:145
#define C4API_BEGIN_DECLS
Definition c4Compat.h:107
#define C4_ASSUME_NONNULL_END
Definition c4Compat.h:37
int64_t C4Timestamp
A date/time representation used for document expiration (and in date/time queries....
Definition c4Base.h:95
struct C4LogObserver C4LogObserver
Opaque handle to a registered logging observer.
Definition c4Base.h:159
FLSliceResult FLStringResult
Definition FLSlice.h:99
FLSlice FLString
Definition FLSlice.h:98
FLSlice FLHeapSlice
A heap-allocated, reference-counted slice.
Definition FLSlice.h:93
CBL_CORE_API bool c4log_getWarnOnErrors(void)
Returns true if warn-on-errors is on; see c4log_warnOnErrors.
CBL_CORE_API C4LogLevel c4log_binaryFileLevel(void)
Returns the minimum level of log messages to be written to the log file, regardless of what level ind...
CBL_CORE_API void c4log_flushLogFiles(void)
Ensures all log messages have been written to the current log files.
CBL_CORE_API const C4LogDomain kC4DatabaseLog
Log domain for database operations.
Definition c4Log.h:47
CBL_CORE_API C4LogLevel c4log_callbackLevel(void)
Returns the minimum level of log messages to be reported via callback, regardless of what level indiv...
CBL_CORE_API const C4LogDomain kC4QueryLog
Log domain for query operations.
Definition c4Log.h:48
CBL_CORE_API void c4log_consoleObserverCallback(const C4LogEntry *, void *context)
A C4LogObserverCallback that logs to stderr, or on Android to __android_log_write.
CBL_CORE_API C4LogLevel c4log_getLevel(C4LogDomain)
Returns the current log level of a domain, the minimum level of message it will log.
CBL_CORE_API FLStringResult c4log_binaryFilePath(void)
Returns the filesystem path of the directory where log files are kept.
CBL_CORE_API C4LogDomain c4log_getDomain(const char *name, bool create)
Looks up a named log domain.
CBL_CORE_API const char * c4log_getDomainName(C4LogDomain)
Returns the name of a log domain.
NODISCARD CBL_CORE_API bool c4log_writeToBinaryFile(C4LogFileOptions options, C4Error *error)
Causes log messages to be written to a file, overwriting any previous contents.
CBL_CORE_API bool c4log_willLog(C4LogDomain, C4LogLevel)
Returns true if logging to this domain at this level will have an effect.
CBL_CORE_API const C4LogDomain kC4SyncLog
Log domain for replication operations.
Definition c4Log.h:49
CBL_CORE_API void c4log_enableFatalExceptionBacktrace(void)
Registers a handler with the C++ runtime that will log a backtrace when an uncaught C++ exception occ...
CBL_CORE_API void CBL_CORE_API void c4vlog(C4LogDomain domain, C4LogLevel level, const char *fmt, va_list args) __printflike(3
Same as c4log, for use in calling functions that already take variable args.
C4LogLevel
Definition c4Log.h:31
CBL_CORE_API const C4LogDomain kC4WebSocketLog
Log domain for WebSocket operations.
Definition c4Log.h:50
NODISCARD CBL_CORE_API C4LogObserver * c4log_replaceObserver(C4LogObserver *oldObs, C4LogObserverConfig config, C4Error *outError)
Atomically unregisters an observer and registers a new one.
CBL_CORE_API void c4log_writeToCallback(C4LogLevel level, C4LogCallback callback, bool preformatted)
Registers (or unregisters) a log callback, and sets the minimum log level to report.
CBL_CORE_API const C4LogDomain kC4DefaultLog
Subsystems that produce logs.
CBL_CORE_API C4LogCallback c4log_getCallback(void)
Returns the current logging callback, or the default one if none has been set.
CBL_CORE_API void c4log_setLevel(C4LogDomain c4Domain, C4LogLevel level)
Changes the level of the given log domain.
CBL_CORE_API void c4log_initConsole(C4LogLevel)
Initializes logging by adding a default observer that writes to stderr, just like c4log_consoleObserv...
CBL_CORE_API C4LogDomain c4log_nextDomain(C4LogDomain)
Returns the next log domain (in arbitrary order) after the given one; or the first domain if the argu...
CBL_CORE_API void c4log_warnOnErrors(bool)
If set to true, LiteCore will log a warning of the form "LiteCore throwing %s error %d: %s" just befo...
CBL_CORE_API void c4log_setBinaryFileLevel(C4LogLevel level)
Sets the minimum level of log messages to be written to the log file.
CBL_CORE_API void CBL_CORE_API void CBL_CORE_API void c4slog(C4LogDomain domain, C4LogLevel level, FLString msg)
Writes a preformatted message to log files, but does not invoke log callbacks.
CBL_CORE_API void c4log_removeObserver(C4LogObserver *)
Unregisters a log observer.
CBL_CORE_API void c4log_setCallbackLevel(C4LogLevel level)
Sets the minimum level of log messages to be reported via callback.
void(* C4LogCallback)(C4LogDomain, C4LogLevel, const char *fmt, va_list args)
A logging callback that the application can register.
Definition c4Log.h:240
CBL_CORE_API void c4logobserver_flush(C4LogObserver *observer)
Ensures all log messages have been written to the observer's files.
NODISCARD CBL_CORE_API C4LogObserver * c4log_newObserver(C4LogObserverConfig config, C4Error *outError)
Creates and registers a log observer, returning a reference.
CBL_CORE_API void c4log(C4LogDomain domain, C4LogLevel level, const char *fmt,...) __printflike(3
Logs a message/warning/error to a specific domain, if its current level is less than or equal to the ...
void(* C4LogObserverCallback)(const C4LogEntry *, void *context)
The callback that will be called by a C4LogObserver.
Definition c4Log.h:78
struct c4LogDomain * C4LogDomain
Reference to a log domain: a specific source of logs that can be enabled or disabled.
Definition c4Log.h:41
@ kC4LogVerbose
Super-verbose messages that are only enabled in debug builds of LiteCore.
Definition c4Log.h:33
@ kC4LogDebug
Definition c4Log.h:32
@ kC4LogNone
Errors that occur; these might be handled internally.
Definition c4Log.h:37
@ kC4LogInfo
More info than you normally want.
Definition c4Log.h:34
@ kC4LogError
Warnings about something unusual that might be a problem.
Definition c4Log.h:36
@ kC4LogWarning
Informational messages.
Definition c4Log.h:35
A (domain, level) pair, used to customize a log observer's configuration.
Definition c4Log.h:71
C4LogDomain domain
Definition c4Log.h:72
C4LogLevel level
Definition c4Log.h:73
An error value.
Definition c4Error.h:133
A log entry, as passed to a C4LogObserverCallback.
Definition c4Log.h:63
C4Timestamp timestamp
Definition c4Log.h:64
C4LogLevel level
Definition c4Log.h:65
FLHeapSlice message
Definition c4Log.h:67
C4LogDomain domain
Definition c4Log.h:66
Configuration for file-based logging.
Definition c4Log.h:53
C4LogLevel log_level
The minimum level of message to be logged.
Definition c4Log.h:54
FLString base_path
The path to the binary log file base name (other elements will be added)
Definition c4Log.h:55
bool use_plaintext
Disables binary encoding of the logs (not recommended)
Definition c4Log.h:58
int32_t max_rotate_count
The maximum amount of old log files to keep.
Definition c4Log.h:57
int64_t max_size_bytes
The maximum size of each log file (minimum 1024)
Definition c4Log.h:56
FLString header
Header text to print at the start of every log file.
Definition c4Log.h:59
Configuration for creating a C4LogObserver, which may either call a callback or write to a file (but ...
Definition c4Log.h:83
void * callbackContext
context value to pass to the callback
Definition c4Log.h:88
C4LogLevel defaultLevel
Log level for domains not listed.
Definition c4Log.h:84
const C4LogFileOptions * fileOptions
Config for file logging (Note: log_level is ignored)
Definition c4Log.h:89
const C4DomainLevel * domains
List of domains and levels (may be NULL if empty)
Definition c4Log.h:85
size_t domainsCount
Length of domains array.
Definition c4Log.h:86
C4LogObserverCallback callback
C callback to invoke.
Definition c4Log.h:87