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 <syslog/global.h>
8
9#include "fx_logger.h"
10
11namespace {
12
13fbl::unique_ptr<fx_logger> g_logger_ptr;
14
15} // namespace
16
17fx_logger_t* fx_log_get_logger() {
18    return g_logger_ptr.get();
19}
20
21zx_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
31zx_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.
48extern 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
53void fx_log_reset_global() {
54    g_logger_ptr.reset(nullptr);
55}
56
57__END_CDECLS
58