Skip to content

Commit b651e35

Browse files
committed
refactor: API cleanup
1 parent 97bad4d commit b651e35

File tree

3 files changed

+98
-122
lines changed

3 files changed

+98
-122
lines changed

datadog-log-ffi/src/lib.rs

Lines changed: 41 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -19,89 +19,65 @@ pub extern "C" fn ddog_logger_set_log_level(
1919
logger_set_log_level(log_level).err().map(Box::new)
2020
}
2121

22-
/// Configures the global logger.
23-
///
24-
/// # Arguments
25-
/// * `config` - Configuration for the logger including level and output destination
22+
/// Disables logging by configuring a no-op logger.
2623
///
2724
/// # Errors
2825
/// Returns an error if the logger cannot be configured.
2926
#[no_mangle]
30-
pub extern "C" fn ddog_logger_configure(config: LoggerConfig) -> Option<Box<Error>> {
31-
logger_configure(logger::LoggerConfig::from(config))
32-
.err()
33-
.map(Box::new)
27+
pub extern "C" fn ddog_logger_disable() -> Option<Box<Error>> {
28+
let config = logger::LoggerConfig {
29+
level: logger::LogEventLevel::Error, // Set to the highest level to minimize logging
30+
writer: logger::WriterConfig::Noop,
31+
};
32+
logger_configure(config).err().map(Box::new)
3433
}
3534

36-
/// Configuration for the logger.
35+
/// Configuration for stdout output.
3736
#[repr(C)]
38-
pub struct LoggerConfig<'a> {
37+
pub struct StdoutConfig {
3938
/// Minimum level for events to be logged
4039
pub level: logger::LogEventLevel,
41-
/// Output configuration
42-
pub writer: WriterConfig<'a>,
4340
}
4441

45-
/// Configuration for log output destination.
46-
#[repr(C)]
47-
pub struct WriterConfig<'a> {
48-
/// Type of output destination
49-
pub kind: WriterKind,
50-
/// File configuration, required when `kind` is [`WriterKind::File`]
51-
pub file: Option<&'a FileConfig<'a>>,
42+
/// Configures the logger to write to stdout with the specified configuration.
43+
///
44+
/// # Arguments
45+
/// * `config` - Configuration for stdout logging including log level
46+
///
47+
/// # Errors
48+
/// Returns an error if the logger cannot be configured.
49+
#[no_mangle]
50+
pub extern "C" fn ddog_logger_configure_stdout(config: StdoutConfig) -> Option<Box<Error>> {
51+
let logger_config = logger::LoggerConfig {
52+
level: config.level,
53+
writer: logger::WriterConfig::Stdout,
54+
};
55+
logger_configure(logger_config).err().map(Box::new)
5256
}
5357

5458
/// Configuration for file output.
5559
#[repr(C)]
5660
pub struct FileConfig<'a> {
5761
/// Path to the log file
5862
pub path: CharSlice<'a>,
63+
/// Minimum level for events to be logged
64+
pub level: logger::LogEventLevel,
5965
}
6066

61-
/// Type of log output destination.
62-
#[repr(C)]
63-
pub enum WriterKind {
64-
/// Discard all output
65-
Noop,
66-
/// Write to standard output
67-
Stdout,
68-
/// Write to file
69-
File,
70-
}
71-
72-
impl From<LoggerConfig<'_>> for logger::LoggerConfig {
73-
fn from(config: LoggerConfig) -> Self {
74-
logger::LoggerConfig {
75-
level: config.level,
76-
writer: logger::WriterConfig::from(config.writer),
77-
}
78-
}
79-
}
80-
81-
impl From<WriterConfig<'_>> for logger::WriterConfig {
82-
fn from(config: WriterConfig) -> Self {
83-
match config.kind {
84-
WriterKind::Noop => logger::WriterConfig::Noop,
85-
WriterKind::Stdout => logger::WriterConfig::Stdout,
86-
WriterKind::File => {
87-
match config.file {
88-
Some(file_config) => {
89-
logger::WriterConfig::File(logger::FileConfig::from(FileConfig {
90-
path: file_config.path,
91-
}))
92-
}
93-
None => logger::WriterConfig::Noop, /* If no file config is provided,
94-
* fallback to Noop */
95-
}
96-
}
97-
}
98-
}
99-
}
100-
101-
impl From<FileConfig<'_>> for logger::FileConfig {
102-
fn from(file_config: FileConfig) -> Self {
103-
logger::FileConfig {
104-
path: file_config.path.to_string(),
105-
}
106-
}
67+
/// Configures the logger to write to a file with the specified configuration.
68+
///
69+
/// # Arguments
70+
/// * `config` - Configuration for file logging including path and log level
71+
///
72+
/// # Errors
73+
/// Returns an error if the logger cannot be configured.
74+
#[no_mangle]
75+
pub extern "C" fn ddog_logger_configure_file(config: FileConfig) -> Option<Box<Error>> {
76+
let logger_config = logger::LoggerConfig {
77+
level: config.level,
78+
writer: logger::WriterConfig::File(logger::FileConfig {
79+
path: config.path.to_string(),
80+
}),
81+
};
82+
logger_configure(logger_config).err().map(Box::new)
10783
}

docs/RFCs/0010-logging-in-libdatadog.md

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,13 @@ This RFC proposes two common logging sinks for libdatadog while keeping the APIs
2929

3030
The logging system provides a simple and flexible public interface for configuring logging behavior in libdatadog. The interface consists of:
3131

32-
* Two primary configuration functions:
33-
* `ddog_logger_configure` - For setting up the logger with desired output destination and level
32+
* Four primary configuration functions:
33+
* `ddog_logger_disable` - For disabling logging completely
34+
* `ddog_logger_configure_stdout` - For setting up console logging
35+
* `ddog_logger_configure_file` - For setting up file-based logging
3436
* `ddog_logger_set_log_level` - For updating the minimum log level at runtime
3537
* These methods must be implemented in a thread-safe manner
36-
* Three supported output destinations:
37-
* Noop - For disabling logging (since libdatadog allows disabling logging)
38+
* Supported output destinations:
3839
* Stdout - For console output
3940
* File - For writing to a specified file
4041
* Configuration structures that provide:
@@ -63,51 +64,52 @@ pub extern "C" fn ddog_logger_set_log_level(
6364
log_level: LogEventLevel,
6465
) -> Option<Box<Error>>;
6566

66-
/// Configures the global logger.
67-
///
68-
/// # Arguments
69-
/// * `config` - Configuration for the logger including level and output destination
67+
/// Disables logging by configuring a no-op logger.
7068
///
7169
/// # Errors
7270
/// Returns an error if the logger cannot be configured.
7371
#[no_mangle]
74-
pub extern "C" fn ddog_logger_configure(config: LoggerConfig) -> Option<Box<Error>>;
72+
pub extern "C" fn ddog_logger_disable() -> Option<Box<Error>>;
7573

76-
/// Configuration for the logger.
74+
/// Configuration for stdout output.
7775
#[repr(C)]
78-
pub struct LoggerConfig<'a> {
76+
pub struct StdoutConfig {
7977
/// Minimum level for events to be logged
8078
pub level: LogEventLevel,
81-
/// Output configuration
82-
pub writer: WriterConfig<'a>,
83-
}
84-
85-
/// Configuration for log output destination.
86-
#[repr(C)]
87-
pub struct WriterConfig<'a> {
88-
/// Type of output destination
89-
pub kind: WriterKind,
90-
/// File configuration, required when `kind` is WriterKind::File
91-
pub file: Option<&'a FileConfig<'a>>,
9279
}
9380

94-
/// Type of log output destination.
95-
#[repr(C)]
96-
pub enum WriterKind {
97-
/// Discard all output
98-
Noop,
99-
/// Write to standard output
100-
Stdout,
101-
/// Write to file
102-
File,
103-
}
81+
/// Configures the logger to write to stdout with the specified configuration.
82+
///
83+
/// # Arguments
84+
/// * `config` - Configuration for stdout logging including log level
85+
///
86+
/// # Errors
87+
/// Returns an error if the logger cannot be configured.
88+
#[no_mangle]
89+
pub extern "C" fn ddog_logger_configure_stdout(
90+
config: StdoutConfig,
91+
) -> Option<Box<Error>>;
10492

10593
/// Configuration for file output.
10694
#[repr(C)]
10795
pub struct FileConfig<'a> {
10896
/// Path to the log file
10997
pub path: CharSlice<'a>,
98+
/// Minimum level for events to be logged
99+
pub level: LogEventLevel,
110100
}
101+
102+
/// Configures the logger to write to a file with the specified configuration.
103+
///
104+
/// # Arguments
105+
/// * `config` - Configuration for file logging including path and log level
106+
///
107+
/// # Errors
108+
/// Returns an error if the logger cannot be configured.
109+
#[no_mangle]
110+
pub extern "C" fn ddog_logger_configure_file(
111+
config: FileConfig,
112+
) -> Option<Box<Error>>;
111113
```
112114

113115
### Example Usage

examples/ffi/trace_exporter.c

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,36 +25,34 @@ void handle_log_error(ddog_Error *err) {
2525
}
2626

2727
int log_init(const char* log_path) {
28-
// Create logger config with debug level
29-
struct ddog_LoggerConfig config = {
30-
.level = DDOG_LOG_EVENT_LEVEL_DEBUG
31-
};
32-
33-
// Setup writer configuration based on log_path
34-
struct ddog_FileConfig file_config;
35-
if (log_path != NULL) {
36-
// Configure file-based logging
37-
file_config.path = (ddog_CharSlice){
38-
.ptr = log_path,
39-
.len = strlen(log_path)
28+
if (log_path == NULL) {
29+
// Configure stdout logging with debug level
30+
struct ddog_StdoutConfig stdout_config = {
31+
.level = DDOG_LOG_EVENT_LEVEL_DEBUG
4032
};
41-
config.writer.kind = DDOG_WRITER_KIND_FILE;
42-
config.writer.file = &file_config;
33+
struct ddog_Error *err = ddog_logger_configure_stdout(stdout_config);
34+
if (err) {
35+
handle_log_error(err);
36+
return 1;
37+
}
4338
} else {
44-
// Configure stdout logging
45-
config.writer.kind = DDOG_WRITER_KIND_STDOUT;
46-
config.writer.file = NULL;
47-
}
48-
49-
// Initialize the logger
50-
struct ddog_Error *err = ddog_logger_configure(config);
51-
if (err) {
52-
handle_log_error(err);
53-
return 1;
39+
// Configure file logging
40+
struct ddog_FileConfig file_config = {
41+
.path = (ddog_CharSlice){
42+
.ptr = log_path,
43+
.len = strlen(log_path)
44+
},
45+
.level = DDOG_LOG_EVENT_LEVEL_DEBUG
46+
};
47+
struct ddog_Error *err = ddog_logger_configure_file(file_config);
48+
if (err) {
49+
handle_log_error(err);
50+
return 1;
51+
}
5452
}
5553

5654
// Set the log level, just for checking the API
57-
err = ddog_logger_set_log_level(DDOG_LOG_EVENT_LEVEL_TRACE);
55+
struct ddog_Error *err = ddog_logger_set_log_level(DDOG_LOG_EVENT_LEVEL_TRACE);
5856
if (err) {
5957
handle_log_error(err);
6058
return 1;

0 commit comments

Comments
 (0)