1 /* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  *     Copyright 2018 Couchbase, Inc.
4  *
5  *   Licensed under the Apache License, Version 2.0 (the "License");
6  *   you may not use this file except in compliance with the License.
7  *   You may obtain a copy of the License at
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *   Unless required by applicable law or agreed to in writing, software
12  *   distributed under the License is distributed on an "AS IS" BASIS,
13  *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *   See the License for the specific language governing permissions and
15  *   limitations under the License.
16  */
17 #include <cbsasl/context.h>
18 #include <cbsasl/logging.h>
19 
20 #include <relaxed_atomic.h>
21 #include <iostream>
22 
23 namespace cb {
24 namespace sasl {
25 namespace logging {
26 
27 cb::RelaxedAtomic<LogCallback> callback;
28 
log(Context* context, Level level, const std::string& message)29 void log(Context* context, Level level, const std::string& message) {
30     auto logger = callback.load();
31     if (logger == nullptr) {
32         return;
33     }
34 
35     if (context == nullptr) {
36         log(level, message);
37         return;
38     }
39 
40     if (level == Level::Error) {
41         // We need to generate UUID
42         std::string full = "UUID:[" + context->getUuid() + "] " + message;
43         callback.load()(level, full);
44     } else {
45         callback.load()(level, message);
46     }
47 }
48 
log(Level level, const std::string& message)49 void log(Level level, const std::string& message) {
50     auto logger = callback.load();
51     if (logger == nullptr) {
52         return;
53     }
54 
55     callback.load()(level, message);
56 }
57 } // namespace logging
58 } // namespace sasl
59 } // namespace cb
60