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 #include <fbl/unique_ptr.h>
6 
7 #include <lib/syslog/global.h>
8 
9 #include "fx_logger.h"
10 
11 namespace {
12 
13 fbl::unique_ptr<fx_logger> g_logger_ptr;
14 
15 } // namespace
16 
fx_log_get_logger()17 fx_logger_t* fx_log_get_logger() {
18     return g_logger_ptr.get();
19 }
20 
fx_log_init(void)21 zx_status_t fx_log_init(void) {
22     fx_logger_config_t config = {.min_severity = FX_LOG_INFO,
23                                  .console_fd = -1,
24                                  .log_service_channel = ZX_HANDLE_INVALID,
25                                  .tags = NULL,
26                                  .num_tags = 0};
27 
28     return fx_log_init_with_config(&config);
29 }
30 
fx_log_init_with_config(const fx_logger_config_t * config)31 zx_status_t fx_log_init_with_config(const fx_logger_config_t* config) {
32     if (config == nullptr) {
33         return ZX_ERR_BAD_STATE;
34     }
35     if (g_logger_ptr.get()) {
36         return ZX_ERR_BAD_STATE;
37     }
38     fx_logger_t* logger = NULL;
39     auto status = fx_logger_create(config, &logger);
40     if (status != ZX_OK) {
41         return status;
42     }
43     g_logger_ptr.reset(logger);
44     return ZX_OK;
45 }
46 
47 // This is here to force a definition to be included here for C99.
48 extern inline bool fx_log_is_enabled(fx_log_severity_t severity);
49 
50 __BEGIN_CDECLS
51 
52 // This clears out global logger. This is used from tests
fx_log_reset_global()53 void fx_log_reset_global() {
54     g_logger_ptr.reset(nullptr);
55 }
56 
57 __END_CDECLS
58