Fix internal tests
[platform/core/security/cert-svc.git] / vcore / dpl / log / src / journal_log_provider.cpp
1 /*
2  *  Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License
15  */
16 /*
17  * @file       journal_log_provider.cpp
18  * @author     Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
19  * @version    1.0
20  */
21
22 #include <dpl/log/journal_log_provider.h>
23 #include <systemd/sd-journal.h>
24 #include <map>
25 #include <stdexcept>
26
27 namespace VcoreDPL {
28 namespace Log {
29
30 namespace {
31 std::map<AbstractLogProvider::LogLevel, int> journalLevel = {
32         { AbstractLogProvider::LogLevel::Error,     LOG_ERR     },
33         { AbstractLogProvider::LogLevel::Warning,   LOG_WARNING },
34         { AbstractLogProvider::LogLevel::Info,      LOG_INFO    },
35         { AbstractLogProvider::LogLevel::Debug,     LOG_DEBUG   },
36         { AbstractLogProvider::LogLevel::Pedantic,  LOG_DEBUG   }
37 };
38
39 } // namespace anonymous
40
41 JournalLogProvider::JournalLogProvider()
42 {}
43
44 JournalLogProvider::~JournalLogProvider()
45 {}
46
47 void JournalLogProvider::Log(AbstractLogProvider::LogLevel level,
48                              const char *message,
49                              const char *fileName,
50                              int line,
51                              const char *function) const
52 {
53     try {
54         sd_journal_send("PRIORITY=%d", journalLevel.at(level),
55                 "CODE_FILE=%s", fileName,
56                 "CODE_FUNC=%s", function,
57                 "CODE_LINE=%d", line,
58                 // add file, line & function info to log message
59                 "MESSAGE=[%s:%d] %s(): %s", fileName, line, function, message,
60                 NULL);
61     } catch (const std::out_of_range&) {
62         sd_journal_send(
63                 "PRIORITY=%d", LOG_ERR,
64                 "CODE_FILE=%s", fileName,
65                 "CODE_FUNC=%s", function,
66                 "CODE_LINE=%d", line,
67                 // add file, line & function info to log message
68                 "MESSAGE=[%s:%d] %s(): Unsupported log level %d", fileName, line, function, level,
69                 NULL);
70     }
71 }
72
73 } /* namespace Log */
74 } /* namespace VcoreDPL */