tslog: modified to support log level 27/203927/6
authorsanghyeok.oh <sanghyeok.oh@samsung.com>
Thu, 18 Apr 2019 06:57:49 +0000 (15:57 +0900)
committersanghyeok.oh <sanghyeok.oh@samsung.com>
Mon, 22 Apr 2019 02:50:33 +0000 (11:50 +0900)
Change-Id: I9f5a4b9cae82737db6037ce56cf7b75bf8e8af0a
Signed-off-by: sanghyeok.oh <sanghyeok.oh@samsung.com>
src/dbuspolicy_serializer.cpp
src/internal/serializer.cpp
src/internal/tslog.cpp
src/internal/tslog.hpp

index 7ca1c41..a08c6a1 100644 (file)
@@ -67,5 +67,7 @@ int main(int argc, char *argv[])
 
        serializer.serialize(input_filename, output);
 
+       cout << "Write " << output.tellp() << " bytes" << endl;
+
        return 0;
 }
index 578157a..ac6841a 100644 (file)
@@ -115,7 +115,7 @@ uint8_t* Serializer::serialize(const ldp_xml::StorageBackendXML &db, size_t &siz
 }
 
 uint8_t* Serializer::serialize(const std::string config_path, size_t &size) {
-       tslog::init();
+       tslog::init(tslog::ldp_log_level::DEFAULT);
        ldp_xml::StorageBackendXML xmlStorage;
 
        if (!xmlStorage.init(config_path.c_str())) {
index aa5a5f9..06ba0c0 100644 (file)
@@ -23,7 +23,7 @@
 
 namespace tslog {
 
-int8_t g_verbosity{-1};
+ldp_log_level g_verbosity{ldp_log_level::SILENT};
 pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;
 
 bool get_log_env(char const *name) {
@@ -31,15 +31,14 @@ bool get_log_env(char const *name) {
        return ldp_log_mode && '0' != *ldp_log_mode;
 }
 
-void init() {
-       g_verbosity = get_log_env("LDP_LOG") ? get_log_env("LDP_VERBOSE") : -1;
+void init(ldp_log_level level) {
+       g_verbosity = get_log_env("LDP_LOG") ? (get_log_env("LDP_VERBOSE") ? ldp_log_level::DEBUG : ldp_log_level::VERBOSE): level;
 }
 
-bool enabled() { return g_verbosity >= 0; }
-bool verbose() { return g_verbosity > 0; }
+bool enabled(ldp_log_level level) { return level <= g_verbosity; }
 
-void flush() {
-       if (tslog::enabled()) {
+void flush(ldp_log_level level) {
+       if (tslog::enabled(level)) {
                pthread_mutex_lock(&g_mutex);
                std::cout << std::flush;
                pthread_mutex_unlock(&g_mutex);
@@ -58,13 +57,25 @@ void logWarning(const std::string &warning)
        LOGW("%s", warning.c_str());
 }
 
+void logDebug(const std::string &debug)
+{
+       log(debug, "\n");
+       LOGD("%s", debug.c_str());
+}
+
+void logVerbose(const std::string &verbose)
+{
+       log_verbose(verbose, "\n");
+       LOGD("%s", verbose.c_str());
+}
+
 std::ostream &operator<<(std::ostream &stream, const print_errno &p) {
        char buf[256];
        return stream << strerror_r(p.err, buf, sizeof(buf));
 }
 
-LogLock::LogLock() {
-       locked = tslog::enabled();
+LogLock::LogLock(ldp_log_level level) {
+       locked = tslog::enabled(level);
        if (locked)
                pthread_mutex_lock(&g_mutex);
 }
index bcf6cf6..f2b4f59 100644 (file)
 
 namespace tslog
 {
+       enum class ldp_log_level {
+               SILENT,
+               ERROR,
+               WARNING,
+               DEFAULT = WARNING,
+               DEBUG,
+               VERBOSE
+       };
        /** Checks value of environmental variable with given name */
        bool get_log_env(char const *name);
 
        /** Checks environmental variables and sets global variable defining if logs are enabled */
-       void init();
+       void init(ldp_log_level level = ldp_log_level::SILENT);
 
        /** Checks if logs are enabled */
-       bool enabled();
-
-       /** Checks if verbosity is enabled */
-       bool verbose();
+       bool enabled(ldp_log_level level = ldp_log_level::DEBUG);
 
-       void flush();
+       void flush(ldp_log_level level = ldp_log_level::ERROR);
 
        void logError(const std::string &error);
        void logWarning(const std::string &warning);
+       void logDebug(const std::string &debug);
+       void logVerbose(const std::string &verbose);
 
        template <typename ...Args>
        void log_to_stream(std::ostream &stream, const Args &...args) {
@@ -52,10 +59,16 @@ namespace tslog
        }
 
        template <typename ...Args>
-       void log(const Args &...args) { if (enabled()) log_to_stream(std::cout, args...); }
+       void log(const Args &...args) { if (enabled(ldp_log_level::DEBUG)) log_to_stream(std::cout, args...); }
+
+       template <typename ...Args>
+       void log_error(const Args &...args) { if (enabled(ldp_log_level::ERROR)) log_to_stream(std::cout, args...); }
+
+       template <typename ...Args>
+       void log_warning(const Args &...args) { if (enabled(ldp_log_level::WARNING)) log_to_stream(std::cout, args...); }
 
        template <typename ...Args>
-       void log_verbose(const Args &...args) { if (verbose()) log_to_stream(std::cout, args...); }
+       void log_verbose(const Args &...args) { if (enabled(ldp_log_level::VERBOSE)) log_to_stream(std::cout, args...); }
 
        struct print_errno {
                int err;
@@ -70,7 +83,7 @@ namespace tslog
        class LogLock {
                bool locked;
        public:
-               LogLock();
+               LogLock(ldp_log_level level = ldp_log_level::ERROR);
                ~LogLock();
        };
 }