From e40f1e32b95aacfd4b292aa3834ccaf6cef9b371 Mon Sep 17 00:00:00 2001 From: yaomanhai Date: Tue, 8 Feb 2022 19:04:56 +0800 Subject: [PATCH 1/5] Code refactor & Persist buffer size config Signed-off-by: yaomanhai --- adapter/properties.cpp | 351 +++++++++------- adapter/properties.h | 32 +- frameworks/native/format.cpp | 2 - frameworks/native/include/format.h | 2 +- frameworks/native/include/hilog_common.h | 2 +- .../include/{hilogtool_msg.h => hilog_msg.h} | 0 services/hilogd/include/log_data.h | 2 +- services/hilogd/include/log_msg_wrapper.h | 2 +- .../hilogd/include/log_persister_rotator.h | 2 +- services/hilogd/service_controller.cpp | 2 +- services/hilogtool/BUILD.gn | 1 + services/hilogtool/include/hilogtool.h | 3 +- services/hilogtool/include/log_controller.h | 4 +- services/hilogtool/include/log_display.h | 7 +- services/hilogtool/include/log_utils.h | 44 ++ services/hilogtool/log_controller.cpp | 389 +++++------------- services/hilogtool/log_display.cpp | 236 ++--------- services/hilogtool/log_utils.cpp | 311 ++++++++++++++ services/hilogtool/main.cpp | 29 +- 19 files changed, 724 insertions(+), 697 deletions(-) rename frameworks/native/include/{hilogtool_msg.h => hilog_msg.h} (100%) create mode 100644 services/hilogtool/include/log_utils.h create mode 100644 services/hilogtool/log_utils.cpp diff --git a/adapter/properties.cpp b/adapter/properties.cpp index 048647f..2cbc86b 100644 --- a/adapter/properties.cpp +++ b/adapter/properties.cpp @@ -32,9 +32,9 @@ #include #include #include +#include #include -#include #include #include @@ -43,18 +43,78 @@ using namespace std; using ReadLock = shared_lock; using InsertLock = unique_lock; +using PropType = enum { + PROP_PRIVATE = 0x01, + PROP_PROCESS_FLOWCTRL, + PROP_DOMAIN_FLOWCTRL, + PROP_GLOBAL_LOG_LEVEL, + PROP_DOMAIN_LOG_LEVEL, + PROP_TAG_LOG_LEVEL, + PROP_SINGLE_DEBUG, + PROP_PERSIST_DEBUG, + PROP_KMSG, + PROP_BUFF_SIZE, + PROP_MAX +}; + +static pthread_mutex_t g_privateLock = PTHREAD_MUTEX_INITIALIZER; +static pthread_mutex_t g_processFlowLock = PTHREAD_MUTEX_INITIALIZER; +static pthread_mutex_t g_domainFlowLock = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t g_globalLevelLock = PTHREAD_MUTEX_INITIALIZER; -static pthread_mutex_t g_tagLevelLock = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t g_domainLevelLock = PTHREAD_MUTEX_INITIALIZER; +static pthread_mutex_t g_tagLevelLock = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t g_debugLock = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t g_persistDebugLock = PTHREAD_MUTEX_INITIALIZER; -static pthread_mutex_t g_privateLock = PTHREAD_MUTEX_INITIALIZER; -static pthread_mutex_t g_processFlowLock = PTHREAD_MUTEX_INITIALIZER; -static pthread_mutex_t g_domainFlowLock = PTHREAD_MUTEX_INITIALIZER; -static pthread_mutex_t g_kmsgLock = PTHREAD_MUTEX_INITIALIZER; -static int LockByProp(uint32_t propType); -static void UnlockByProp(uint32_t propType); +//Only frequent using properties need cache vaule and then need lock +static pthread_mutex_t *g_locks[] = { + nullptr, + &g_privateLock, // PROP_PRIVATE = 0x01 + &g_processFlowLock, // PROP_PROCESS_FLOWCTRL, + &g_domainFlowLock, // PROP_DOMAIN_FLOWCTRL, + &g_globalLevelLock, // PROP_GLOBAL_LOG_LEVEL, + &g_domainLevelLock, // PROP_DOMAIN_LOG_LEVEL, + &g_tagLevelLock, //PROP_TAG_LOG_LEVEL, + &g_debugLock, // PROP_SINGLE_DEBUG, + &g_persistDebugLock, // PROP_PERSIST_DEBUG, + nullptr, // PROP_KMSG, + nullptr, // PROP_BUFF_SIZE, + nullptr, // PROP_MAX +}; + +static const int HILOG_PROP_VALUE_MAX = 92; + +static int LockByProp(uint32_t propType) +{ + if(propType < PROP_MAX && nullptr != g_locks[propType]){ + return pthread_mutex_trylock(g_locks[propType]); + } + return -1; +} + +static void UnlockByProp(uint32_t propType){ + if(propType < PROP_MAX && nullptr != g_locks[propType]){ + pthread_mutex_unlock(g_locks[propType]); + } +} + +static string GetPropertyName(uint32_t propType) +{ + switch (propType) { + case PROP_PRIVATE: return "hilog.private.on"; + case PROP_PROCESS_FLOWCTRL: return "hilog.flowctrl.pid.on"; + case PROP_DOMAIN_FLOWCTRL: return "hilog.flowctrl.domain.on"; + case PROP_GLOBAL_LOG_LEVEL: return "hilog.loggable.global"; + case PROP_DOMAIN_LOG_LEVEL: return "hilog.loggable.domain."; + case PROP_TAG_LOG_LEVEL: return "hilog.loggable.tag."; + case PROP_SINGLE_DEBUG: return "hilog.debug.on"; + case PROP_KMSG: return "persist.sys.hilog.kmsg.on"; + case PROP_PERSIST_DEBUG: return "persist.sys.hilog.debug.on"; + case PROP_BUFF_SIZE: return "persist.sys.hilog.buffersize."; + default: break; + } + return ""; +} class PropertyTypeLocker { public: @@ -99,9 +159,9 @@ public: T getValue() { - if (m_handle == -1) { + if (m_handle == (unsigned int)-1) { auto handle = FindParameter(m_key.c_str()); - if (handle == -1) { + if (handle == (unsigned int)-1) { return m_defaultValue; } m_handle = handle; @@ -161,8 +221,8 @@ private: using SwitchCache = CacheData; using LogLevelCache = CacheData; - -void PropertyGet(const string &key, char *value, int len) +/* +static void PropertyGet(const string &key, char *value, int len) { if (len > HILOG_PROP_VALUE_MAX) { std::cerr << "PropertyGet(): len exceed maximum.\n"; @@ -181,11 +241,11 @@ void PropertyGet(const string &key, char *value, int len) } } -void PropertySet(const string &key, const char* value) +static void PropertySet(const string &key, const char* value) { auto len = value ? strlen(value) : 0; if (len > HILOG_PROP_VALUE_MAX) { - std::cerr << "PropertyGet(): len exceed maximum.\n"; + std::cerr << "PropertySet(): len exceed maximum.\n"; return; } @@ -198,6 +258,7 @@ void PropertySet(const string &key, const char* value) } } } +*/ string GetProgName() { @@ -208,105 +269,7 @@ string GetProgName() #endif } -string GetPropertyName(uint32_t propType) -{ - string key; - switch (propType) { - case PROP_PRIVATE: - key = "hilog.private.on"; - break; - case PROP_PROCESS_FLOWCTRL: - key = "hilog.flowctrl.pid.on"; - break; - case PROP_DOMAIN_FLOWCTRL: - key = "hilog.flowctrl.domain.on"; - break; - case PROP_GLOBAL_LOG_LEVEL: - key = "hilog.loggable.global"; - break; - case PROP_DOMAIN_LOG_LEVEL: - key = "hilog.loggable.domain."; - break; - case PROP_TAG_LOG_LEVEL: - key = "hilog.loggable.tag."; - break; - case PROP_SINGLE_DEBUG: - key = "hilog.debug.on"; - break; - case PROP_KMSG: - key = "persist.sys.hilog.kmsg.on"; - break; - case PROP_PERSIST_DEBUG: - key = "persist.sys.hilog.debug.on"; - break; - default: - break; - } - return key; -} - -static int LockByProp(uint32_t propType) -{ - switch (propType) { - case PROP_PRIVATE: - return pthread_mutex_trylock(&g_privateLock); - case PROP_PROCESS_FLOWCTRL: - return pthread_mutex_trylock(&g_processFlowLock); - case PROP_DOMAIN_FLOWCTRL: - return pthread_mutex_trylock(&g_domainFlowLock); - case PROP_GLOBAL_LOG_LEVEL: - return pthread_mutex_trylock(&g_globalLevelLock); - case PROP_DOMAIN_LOG_LEVEL: - return pthread_mutex_trylock(&g_domainLevelLock); - case PROP_TAG_LOG_LEVEL: - return pthread_mutex_trylock(&g_tagLevelLock); - case PROP_SINGLE_DEBUG: - return pthread_mutex_trylock(&g_debugLock); - case PROP_KMSG: - return pthread_mutex_trylock(&g_kmsgLock); - case PROP_PERSIST_DEBUG: - return pthread_mutex_trylock(&g_persistDebugLock); - default: - return -1; - } -} - -static void UnlockByProp(uint32_t propType) -{ - switch (propType) { - case PROP_PRIVATE: - pthread_mutex_unlock(&g_privateLock); - break; - case PROP_PROCESS_FLOWCTRL: - pthread_mutex_unlock(&g_processFlowLock); - break; - case PROP_DOMAIN_FLOWCTRL: - pthread_mutex_unlock(&g_domainFlowLock); - break; - case PROP_GLOBAL_LOG_LEVEL: - pthread_mutex_unlock(&g_globalLevelLock); - break; - case PROP_DOMAIN_LOG_LEVEL: - pthread_mutex_unlock(&g_domainLevelLock); - break; - case PROP_TAG_LOG_LEVEL: - pthread_mutex_unlock(&g_tagLevelLock); - break; - case PROP_SINGLE_DEBUG: - pthread_mutex_unlock(&g_debugLock); - break; - case PROP_KMSG: - pthread_mutex_unlock(&g_kmsgLock); - break; - case PROP_PERSIST_DEBUG: - pthread_mutex_unlock(&g_persistDebugLock); - break; - default: - break; - } -} - -static bool textToBool(const RawPropertyData& data, bool defaultVal) +static bool TextToBool(const RawPropertyData& data, bool defaultVal) { if (!strcmp(data.data(), "true")) { return true; @@ -323,80 +286,77 @@ bool IsDebugOn() bool IsSingleDebugOn() { - static auto *switchCache = new SwitchCache(textToBool, false, PROP_SINGLE_DEBUG); + static auto *switchCache = new SwitchCache(TextToBool, false, PROP_SINGLE_DEBUG); return switchCache->getValue(); } bool IsPersistDebugOn() { - static auto *switchCache = new SwitchCache(textToBool, false, PROP_PERSIST_DEBUG); + static auto *switchCache = new SwitchCache(TextToBool, false, PROP_PERSIST_DEBUG); return switchCache->getValue(); } bool IsPrivateSwitchOn() { - static auto *switchCache = new SwitchCache(textToBool, true, PROP_PRIVATE); + static auto *switchCache = new SwitchCache(TextToBool, true, PROP_PRIVATE); return switchCache->getValue(); } bool IsProcessSwitchOn() { - static auto *switchCache = new SwitchCache(textToBool, false, PROP_PROCESS_FLOWCTRL); + static auto *switchCache = new SwitchCache(TextToBool, false, PROP_PROCESS_FLOWCTRL); return switchCache->getValue(); } bool IsDomainSwitchOn() { - static auto *switchCache = new SwitchCache(textToBool, false, PROP_DOMAIN_FLOWCTRL); + static auto *switchCache = new SwitchCache(TextToBool, false, PROP_DOMAIN_FLOWCTRL); return switchCache->getValue(); } bool IsKmsgSwitchOn() { - static auto *switchCache = new SwitchCache(textToBool, false, PROP_KMSG); + static auto *switchCache = new SwitchCache(TextToBool, false, PROP_KMSG); return switchCache->getValue(); } -static uint16_t textToLogLevel(const RawPropertyData& data, uint16_t defaultVal) +static uint16_t TextToLogLevel(const RawPropertyData& data, uint16_t defaultVal) { - static const std::unordered_map logLevels = { - { 'd', LOG_DEBUG }, { 'D', LOG_DEBUG }, - { 'i', LOG_INFO }, { 'I', LOG_INFO }, - { 'w', LOG_WARN }, { 'W', LOG_WARN }, - { 'e', LOG_ERROR }, { 'E', LOG_ERROR }, - { 'f', LOG_FATAL }, { 'F', LOG_FATAL }, - }; - auto it = logLevels.find(data[0]); - if (it != logLevels.end()) { - return it->second; + switch (data[0]) + { + case 'd': case 'D': return LOG_DEBUG; + case 'i': case 'I': return LOG_INFO; + case 'w': case 'W': return LOG_WARN; + case 'e': case 'E': return LOG_ERROR; + case 'f': case 'F': return LOG_FATAL; + default: break; } return LOG_LEVEL_MIN; } uint16_t GetGlobalLevel() { - static auto *logLevelCache = new LogLevelCache(textToLogLevel, LOG_LEVEL_MIN, PROP_GLOBAL_LOG_LEVEL); + static auto *logLevelCache = new LogLevelCache(TextToLogLevel, LOG_LEVEL_MIN, PROP_GLOBAL_LOG_LEVEL); return logLevelCache->getValue(); } -uint16_t GetDomainLevel(uint32_t domain) +uint16_t GetTagLevel(const string& tag) { - static auto *domainMap = new std::unordered_map(); + static auto *tagMap = new std::unordered_map(); static shared_timed_mutex* mtx = new shared_timed_mutex; - std::decay::type::iterator it; + std::decay::type::iterator it; { ReadLock lock(*mtx); - it = domainMap->find(domain); + it = tagMap->find(tag); } - if (it == domainMap->end()) { // new domain + if (it == tagMap->end()) { // new tag InsertLock lock(*mtx); - it = domainMap->find(domain); // secured for two thread went across above condition - if (it == domainMap->end()) { - LogLevelCache* levelCache = new LogLevelCache(textToLogLevel, LOG_LEVEL_MIN, PROP_DOMAIN_LOG_LEVEL, - to_string(domain)); - auto result = domainMap->insert({ domain, levelCache }); + it = tagMap->find(tag); // secured for two thread went across above condition + if (it == tagMap->end()) { + LogLevelCache* levelCache = new LogLevelCache(TextToLogLevel, LOG_LEVEL_MIN, PROP_TAG_LOG_LEVEL, tag); + auto result = tagMap->insert({ tag, levelCache }); if (!result.second) { - std::cerr << "Can't insert new LogLevelCache for domain: " << domain << "\n"; + std::cerr << "Can't insert new LogLevelCache for tag: " << tag << "\n"; return LOG_LEVEL_MIN; } it = result.first; @@ -406,23 +366,24 @@ uint16_t GetDomainLevel(uint32_t domain) return levelCache->getValue(); } -uint16_t GetTagLevel(const string& tag) +uint16_t GetDomainLevel(uint32_t domain) { - static auto *tagMap = new std::unordered_map(); + static auto *domainMap = new std::unordered_map(); static shared_timed_mutex* mtx = new shared_timed_mutex; - std::decay::type::iterator it; + std::decay::type::iterator it; { ReadLock lock(*mtx); - it = tagMap->find(tag); + it = domainMap->find(domain); } - if (it == tagMap->end()) { // new tag + if (it == domainMap->end()) { // new domain InsertLock lock(*mtx); - it = tagMap->find(tag); // secured for two thread went across above condition - if (it == tagMap->end()) { - LogLevelCache* levelCache = new LogLevelCache(textToLogLevel, LOG_LEVEL_MIN, PROP_TAG_LOG_LEVEL, tag); - auto result = tagMap->insert({ tag, levelCache }); + it = domainMap->find(domain); // secured for two thread went across above condition + if (it == domainMap->end()) { + LogLevelCache* levelCache = new LogLevelCache(TextToLogLevel, LOG_LEVEL_MIN, PROP_DOMAIN_LOG_LEVEL, + Int2HexStr(domain)); + auto result = domainMap->insert({ domain, levelCache }); if (!result.second) { - std::cerr << "Can't insert new LogLevelCache for tag: " << tag << "\n"; + std::cerr << "Can't insert new LogLevelCache for domain: " << domain << "\n"; return LOG_LEVEL_MIN; } it = result.first; @@ -431,3 +392,89 @@ uint16_t GetTagLevel(const string& tag) LogLevelCache* levelCache = it->second; return levelCache->getValue(); } + +static int SetBoolValue(PropType type, bool val) +{ + string key = GetPropertyName(type); + return key == "" ? -1 : (SetParameter(key.c_str(), val ? "true" : "false")); +} + +int SetSingleDebugOn(bool on) +{ + return SetBoolValue(PROP_SINGLE_DEBUG, on); +} + +int SetPersistDebugOn(bool on) +{ + return SetBoolValue(PROP_PERSIST_DEBUG, on); +} + +int SetPrivateSwitchOn(bool on) +{ + return SetBoolValue(PROP_PRIVATE, on); +} + +int SetProcessSwitchOn(bool on) +{ + return SetBoolValue(PROP_PROCESS_FLOWCTRL, on); +} + +int SetDomainSwitchOn(bool on) +{ + return SetBoolValue(PROP_DOMAIN_FLOWCTRL, on); +} + +int SetKmsgSwitchOn(bool on) +{ + return SetBoolValue(PROP_KMSG, on); +} + +static const string LevelToText(LogLevel lvl) +{ + switch (lvl) + { + case LOG_DEBUG: return "D"; + case LOG_INFO: return "I"; + case LOG_WARN: return "W"; + case LOG_ERROR: return "E"; + case LOG_FATAL: return "F"; + default: break; + } + return "D"; +} + +static int SetLevel(PropType type, const string& suffix, LogLevel lvl) +{ + string key = GetPropertyName(type) + suffix; + return key == "" ? -1 : (SetParameter(key.c_str(), LevelToText(lvl).c_str())); +} + +int SetGlobalLevel(LogLevel lvl) +{ + return SetLevel(PROP_GLOBAL_LOG_LEVEL, "", lvl); +} + +int SetTagLevel(const std::string& tag, LogLevel lvl) +{ + return SetLevel(PROP_TAG_LOG_LEVEL, tag, lvl); +} + +int SetDomainLevel(uint32_t domain, LogLevel lvl) +{ + return SetLevel(PROP_DOMAIN_LOG_LEVEL, Int2HexStr(domain), lvl); +} + +string Int2HexStr(int i){ + std::stringstream ss; + ss << std::hex << i; + return ss.str(); +} + +int HexStr2Int(const string& str) +{ + int i = 0; + std::stringstream ss; + ss << std::hex << str; + ss >> i; + return i; +} \ No newline at end of file diff --git a/adapter/properties.h b/adapter/properties.h index 12d2e5d..170b31e 100644 --- a/adapter/properties.h +++ b/adapter/properties.h @@ -20,25 +20,10 @@ #include #include -static const int HILOG_PROP_VALUE_MAX = 92; +#include -using PropType = enum { - PROP_PRIVATE = 0x01, - PROP_PROCESS_FLOWCTRL, - PROP_DOMAIN_FLOWCTRL, - PROP_GLOBAL_LOG_LEVEL, - PROP_DOMAIN_LOG_LEVEL, - PROP_TAG_LOG_LEVEL, - PROP_SINGLE_DEBUG, - PROP_KMSG, - PROP_PERSIST_DEBUG, -}; - -std::string GetPropertyName(uint32_t propType); std::string GetProgName(); -uint16_t GetTagLevel(const std::string& tag); -void PropertyGet(const std::string &key, char *value, int len); -void PropertySet(const std::string &key, const char* value); + bool IsDebugOn(); bool IsSingleDebugOn(); bool IsPersistDebugOn(); @@ -47,6 +32,19 @@ bool IsProcessSwitchOn(); bool IsDomainSwitchOn(); bool IsKmsgSwitchOn(); uint16_t GetGlobalLevel(); +uint16_t GetTagLevel(const std::string& tag); uint16_t GetDomainLevel(uint32_t domain); +int SetSingleDebugOn(bool on); +int SetPersistDebugOn(bool on); +int SetPrivateSwitchOn(bool on); +int SetProcessSwitchOn(bool on); +int SetDomainSwitchOn(bool on); +int SetKmsgSwitchOn(bool on); +int SetGlobalLevel(LogLevel lvl); +int SetTagLevel(const std::string& tag, LogLevel lvl); +int SetDomainLevel(uint32_t domain, LogLevel lvl); + +std::string Int2HexStr(int i); +int HexStr2Int(const std::string& str); #endif diff --git a/frameworks/native/format.cpp b/frameworks/native/format.cpp index 5126e9f..bf7fdbc 100644 --- a/frameworks/native/format.cpp +++ b/frameworks/native/format.cpp @@ -14,10 +14,8 @@ */ #include "format.h" #include "hilog/log.h" -#include "hilogtool_msg.h" #include "hilog_common.h" - #include #include #include diff --git a/frameworks/native/include/format.h b/frameworks/native/include/format.h index a3a6923..1dd7f63 100644 --- a/frameworks/native/include/format.h +++ b/frameworks/native/include/format.h @@ -16,7 +16,7 @@ #define LOG_FORMAT_H #include #include "hilog_common.h" -#include "hilogtool_msg.h" +#include "hilog_msg.h" namespace OHOS { namespace HiviewDFX { const char* ParsedFromLevel(uint16_t level); diff --git a/frameworks/native/include/hilog_common.h b/frameworks/native/include/hilog_common.h index 9fff022..829ab4f 100644 --- a/frameworks/native/include/hilog_common.h +++ b/frameworks/native/include/hilog_common.h @@ -37,7 +37,7 @@ #define MAX_PIDS 5 #define RET_SUCCESS 0 #define RET_FAIL (-1) -#define IS_ONE(number, n) ((number>>n)&0x01) +//#define IS_ONE(number, n) ((number>>n)&0x01) #define ONE_KB (1UL<<10) #define ONE_MB (1UL<<20) #define ONE_GB (1UL<<30) diff --git a/frameworks/native/include/hilogtool_msg.h b/frameworks/native/include/hilog_msg.h similarity index 100% rename from frameworks/native/include/hilogtool_msg.h rename to frameworks/native/include/hilog_msg.h diff --git a/services/hilogd/include/log_data.h b/services/hilogd/include/log_data.h index 1544646..8ad5333 100644 --- a/services/hilogd/include/log_data.h +++ b/services/hilogd/include/log_data.h @@ -21,7 +21,7 @@ #include #include -#include "hilogtool_msg.h" +#include "hilog_msg.h" namespace OHOS { namespace HiviewDFX { diff --git a/services/hilogd/include/log_msg_wrapper.h b/services/hilogd/include/log_msg_wrapper.h index e2b01e2..98ac7b0 100644 --- a/services/hilogd/include/log_msg_wrapper.h +++ b/services/hilogd/include/log_msg_wrapper.h @@ -21,7 +21,7 @@ #include #include -#include "hilogtool_msg.h" +#include "hilog_msg.h" namespace OHOS { namespace HiviewDFX { diff --git a/services/hilogd/include/log_persister_rotator.h b/services/hilogd/include/log_persister_rotator.h index 1e85f9c..f5f92d4 100644 --- a/services/hilogd/include/log_persister_rotator.h +++ b/services/hilogd/include/log_persister_rotator.h @@ -19,7 +19,7 @@ #include #include "hilog_common.h" -#include "hilogtool_msg.h" +#include "hilog_msg.h" #include "log_filter.h" namespace OHOS { namespace HiviewDFX { diff --git a/services/hilogd/service_controller.cpp b/services/hilogd/service_controller.cpp index 8da7643..3351ece 100644 --- a/services/hilogd/service_controller.cpp +++ b/services/hilogd/service_controller.cpp @@ -38,7 +38,7 @@ #include "hilog/log.h" #include "hilog_common.h" #include "log_data.h" -#include "hilogtool_msg.h" +#include "hilog_msg.h" #include "log_buffer.h" #include "log_persister.h" diff --git a/services/hilogtool/BUILD.gn b/services/hilogtool/BUILD.gn index 358dd70..cb00487 100644 --- a/services/hilogtool/BUILD.gn +++ b/services/hilogtool/BUILD.gn @@ -23,6 +23,7 @@ ohos_executable("hilog") { sources = [ "log_controller.cpp", "log_display.cpp", + "log_utils.cpp", "main.cpp", ] diff --git a/services/hilogtool/include/hilogtool.h b/services/hilogtool/include/hilogtool.h index ef74cc9..4042a6d 100644 --- a/services/hilogtool/include/hilogtool.h +++ b/services/hilogtool/include/hilogtool.h @@ -65,4 +65,5 @@ typedef struct { } HilogArgs; } // namespace HiviewDFX } // namespace OHOS -#endif + +#endif // HILOGTOOL_H diff --git a/services/hilogtool/include/log_controller.h b/services/hilogtool/include/log_controller.h index c9369fc..fb69659 100644 --- a/services/hilogtool/include/log_controller.h +++ b/services/hilogtool/include/log_controller.h @@ -20,9 +20,9 @@ #include #include -#include "hilogtool_msg.h" -#include "seq_packet_socket_client.h" +#include "hilog_msg.h" #include "hilogtool.h" +#include "seq_packet_socket_client.h" namespace OHOS { namespace HiviewDFX { diff --git a/services/hilogtool/include/log_display.h b/services/hilogtool/include/log_display.h index b92025b..7d1babc 100644 --- a/services/hilogtool/include/log_display.h +++ b/services/hilogtool/include/log_display.h @@ -17,17 +17,16 @@ #define LOG_DISPLAY_H #include "hilog_common.h" -#include "hilogtool_msg.h" -#include "log_controller.h" +#include "hilog_msg.h" +#include "hilogtool.h" namespace OHOS { namespace HiviewDFX { using namespace std; int32_t ControlCmdResult(const char* message); -std::string ParseErrorCode(ErrorCode errorCode); void HilogShowLog(uint32_t showFormat, HilogDataMessage* contentOut, const HilogArgs* context, vector& tailBuffer); -HilogShowFormat HilogFormat (const char* formatArg); } // namespace HiviewDFX } // namespace OHOS + #endif diff --git a/services/hilogtool/include/log_utils.h b/services/hilogtool/include/log_utils.h new file mode 100644 index 0000000..f4d5e0a --- /dev/null +++ b/services/hilogtool/include/log_utils.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +#ifndef LOG_UTILS_H +#define LOG_UTILS_H + +#include +#include + +namespace OHOS { +namespace HiviewDFX { +std::string ErrorCode2Str(uint16_t errorCode); +std::string LogType2Str(uint16_t logType); +uint16_t Str2LogType(const std::string& str); +std::string ComboLogType2Str(uint16_t shiftType); +uint16_t Str2ComboLogType(const std::string& str); +std::string LogLevel2Str(uint16_t logLevel); +uint16_t Str2LogLevel(const std::string& str); +std::string CompressType2Str(uint16_t compressType); +uint16_t Str2CompressType(const std::string& str); +std::string ShowFormat2Str(uint16_t showFormat); +uint16_t Str2ShowFormat(const std::string& str); +std::string Size2Str(uint64_t size); +uint64_t Str2Size(const std::string& str); + +constexpr char DEFAULT_SPLIT_DELIMIT[] = ","; +void Split(const std::string& src, const std::string& separator, std::vector& dest); +uint32_t GetBitsCount(uint64_t n); +} // namespace HiviewDFX +} // namespace OHOS +#endif // LOG_UTILS_H \ No newline at end of file diff --git a/services/hilogtool/log_controller.cpp b/services/hilogtool/log_controller.cpp index cb27e04..1db04c9 100644 --- a/services/hilogtool/log_controller.cpp +++ b/services/hilogtool/log_controller.cpp @@ -23,11 +23,9 @@ #include #include #include -#include "hilog/log.h" -#include "hilog_common.h" -#include "hilogtool_msg.h" #include "seq_packet_socket_client.h" #include "properties.h" +#include "log_utils.h" #include "log_display.h" namespace OHOS { @@ -48,118 +46,6 @@ void SetMsgHead(MessageHeader* msgHeader, const uint8_t msgCmd, const uint16_t m msgHeader->msgLen = msgLen; } -void Split(const std::string& src, const std::string& separator, std::vector& dest) -{ - string str = src; - string substring; - string::size_type start = 0; - string::size_type index; - dest.clear(); - index = str.find_first_of(separator, start); - if (index == string::npos) { - dest.push_back(str); - return; - } - do { - substring = str.substr(start, index - start); - dest.push_back(substring); - start = index + separator.size(); - index = str.find(separator, start); - if (start == string::npos) { - break; - } - } while (index != string::npos); - substring = str.substr(start); - dest.emplace_back(substring); -} - -uint16_t GetLogType(const string& logTypeStr) -{ - uint16_t logType; - if (logTypeStr == "init") { - logType = LOG_INIT; - } else if (logTypeStr == "core") { - logType = LOG_CORE; - } else if (logTypeStr == "app") { - logType = LOG_APP; - } else if (logTypeStr == "kmsg") { - logType = LOG_KMSG; - } else { - return 0xffff; - } - return logType; -} - -uint64_t GetBuffSize(const string& buffSizeStr) -{ - uint64_t index = buffSizeStr.size() - 1; - uint64_t buffSize; - std::regex reg("[0-9]+[bBkKmMgGtT]?"); - if (!std::regex_match(buffSizeStr, reg)) { - std::cout << ParseErrorCode(ERR_BUFF_SIZE_INVALID) << std::endl; - exit(-1); - } - if (buffSizeStr[index] == 'b' || buffSizeStr[index] == 'B') { - buffSize = stol(buffSizeStr.substr(0, index)); - } else if (buffSizeStr[index] == 'k' || buffSizeStr[index] == 'K') { - buffSize = stol(buffSizeStr.substr(0, index)) * ONE_KB; - } else if (buffSizeStr[index] == 'm' || buffSizeStr[index] == 'M') { - buffSize = stol(buffSizeStr.substr(0, index)) * ONE_MB; - } else if (buffSizeStr[index] == 'g' || buffSizeStr[index] == 'G') { - buffSize = stol(buffSizeStr.substr(0, index)) * ONE_GB; - } else if (buffSizeStr[index] == 't' || buffSizeStr[index] == 'T') { - buffSize = stol(buffSizeStr.substr(0, index)) * ONE_TB; - } else { - buffSize = stol(buffSizeStr.substr(0, index + 1)); - } - return buffSize; -} - -uint16_t GetCompressAlg(const std::string& pressAlg) -{ - if (pressAlg == "none") { - return COMPRESS_TYPE_NONE; - } else if (pressAlg == "zlib") { - return COMPRESS_TYPE_ZLIB; - } else if (pressAlg == "zstd") { - return COMPRESS_TYPE_ZSTD; - } - return COMPRESS_TYPE_ZLIB; -} - -uint16_t GetLogLevel(const std::string& logLevelStr, std::string& logLevel) -{ - if (logLevelStr == "debug" || logLevelStr == "DEBUG" || logLevelStr == "d" || logLevelStr == "D") { - logLevel = "D"; - return LOG_DEBUG; - } else if (logLevelStr == "info" || logLevelStr == "INFO" || logLevelStr == "i" || logLevelStr == "I") { - logLevel = "I"; - return LOG_INFO; - } else if (logLevelStr == "warn" || logLevelStr == "WARN" || logLevelStr == "w" || logLevelStr == "W") { - logLevel = "W"; - return LOG_WARN; - } else if (logLevelStr == "error" || logLevelStr == "ERROR" || logLevelStr == "e" || logLevelStr == "E") { - logLevel = "E"; - return LOG_ERROR; - } else if (logLevelStr == "fatal" || logLevelStr == "FATAL" || logLevelStr == "f" || logLevelStr == "F") { - logLevel = "F"; - return LOG_FATAL; - } - return 0xffff; -} - -string SetDefaultLogType(const std::string& logTypeStr) -{ - string logType; - if (logTypeStr == "") { - logType = "core app"; - } else if (logTypeStr == "all") { - logType = "core app init"; - } else { - logType = logTypeStr; - } - return logType; -} void NextRequestOp(SeqPacketSocketClient& controller, uint16_t sendId) { NextRequest nextRequest = {{0}}; @@ -256,12 +142,13 @@ int32_t BufferSizeOp(SeqPacketSocketClient& controller, uint8_t msgCmd, const st const std::string& buffSizeStr) { char msgToSend[MSG_MAX_LEN] = {0}; - vector vecLogType; - uint32_t logTypeNum; + uint16_t logType = Str2ComboLogType(logTypeStr); + if (LOG_TYPE_MAX == logType) { + cout << ErrorCode2Str(ERR_LOG_TYPE_INVALID) << endl; + return RET_FAIL; + } + uint32_t logTypeNum = GetBitsCount(logType); uint32_t iter; - string logType = SetDefaultLogType(logTypeStr); - Split(logType, " ", vecLogType); - logTypeNum = vecLogType.size(); switch (msgCmd) { case MC_REQ_BUFFER_SIZE: { BufferSizeRequest* pBuffSizeReq = reinterpret_cast(msgToSend); @@ -270,11 +157,8 @@ int32_t BufferSizeOp(SeqPacketSocketClient& controller, uint8_t msgCmd, const st return RET_FAIL; } for (iter = 0; iter < logTypeNum; iter++) { - pBuffSizeMsg->logType = GetLogType(vecLogType[iter]); - if (pBuffSizeMsg->logType == 0xffff) { - cout << ParseErrorCode(ERR_LOG_TYPE_INVALID) << endl; - return RET_FAIL; - } + pBuffSizeMsg->logType = logType & (~ (logType - 1)); //Get first bit 1 - logType + logType &= (~pBuffSizeMsg->logType); pBuffSizeMsg++; } SetMsgHead(&pBuffSizeReq->msgHeader, msgCmd, sizeof(BuffSizeMsg) * logTypeNum); @@ -289,12 +173,9 @@ int32_t BufferSizeOp(SeqPacketSocketClient& controller, uint8_t msgCmd, const st return RET_FAIL; } for (iter = 0; iter < logTypeNum; iter++) { - pBuffResizeMsg->logType = GetLogType(vecLogType[iter]); - if (pBuffResizeMsg->logType == 0xffff) { - cout << ParseErrorCode(ERR_LOG_TYPE_INVALID) << endl; - return RET_FAIL; - } - pBuffResizeMsg->buffSize = GetBuffSize(buffSizeStr); + pBuffResizeMsg->logType = logType & (~ (logType - 1)); //Get first bit 1 - logType + logType &= (~pBuffResizeMsg->logType); + pBuffResizeMsg->buffSize = Str2Size(buffSizeStr); pBuffResizeMsg++; } SetMsgHead(&pBuffResizeReq->msgHeader, msgCmd, sizeof(BuffResizeMsg) * logTypeNum); @@ -314,20 +195,22 @@ int32_t StatisticInfoOp(SeqPacketSocketClient& controller, uint8_t msgCmd, if ((logTypeStr != "" && domainStr != "") || (logTypeStr == "" && domainStr == "")) { return RET_FAIL; } - uint16_t logType = GetLogType(logTypeStr); + uint16_t logType = Str2ComboLogType(logTypeStr); uint32_t domain; + + if (logType == LOG_TYPE_MAX) { + cout << ErrorCode2Str(ERR_LOG_TYPE_INVALID) << endl; + return RET_FAIL; + } + if (domainStr == "") { domain = 0xffffffff; - if (logType == 0xffff) { - cout << ParseErrorCode(ERR_LOG_TYPE_INVALID) << endl; - return RET_FAIL; - } } else { - std::istringstream(domainStr) >> domain; - if (domain == 0 || domain > DOMAIN_MAX_SCOPE) { - cout << ParseErrorCode(ERR_DOMAIN_INVALID) << endl; - return RET_FAIL; - } + domain = HexStr2Int(domainStr); + } + if (domain == 0 || domain > DOMAIN_MAX_SCOPE) { + cout << ErrorCode2Str(ERR_DOMAIN_INVALID) << endl; + return RET_FAIL; } switch (msgCmd) { case MC_REQ_STATISTIC_INFO_QUERY: { @@ -355,28 +238,26 @@ int32_t StatisticInfoOp(SeqPacketSocketClient& controller, uint8_t msgCmd, int32_t LogClearOp(SeqPacketSocketClient& controller, uint8_t msgCmd, const std::string& logTypeStr) { char msgToSend[MSG_MAX_LEN] = {0}; - vector vecLogType; - uint32_t logTypeNum; + uint16_t logType = Str2ComboLogType(logTypeStr); + if (LOG_TYPE_MAX == logType) { + cout << ErrorCode2Str(ERR_LOG_TYPE_INVALID) << endl; + return RET_FAIL; + } + uint32_t logTypeNum = GetBitsCount(logType); uint32_t iter; - string logType = SetDefaultLogType(logTypeStr); - Split(logType, " ", vecLogType); - logTypeNum = vecLogType.size(); LogClearRequest* pLogClearReq = reinterpret_cast(msgToSend); LogClearMsg* pLogClearMsg = reinterpret_cast(&pLogClearReq->logClearMsg); if (!pLogClearMsg) { - cout << ParseErrorCode(ERR_MEM_ALLOC_FAIL) << endl; + cout << ErrorCode2Str(ERR_MEM_ALLOC_FAIL) << endl; return RET_FAIL; } if (logTypeNum * sizeof(LogClearMsg) + sizeof(MessageHeader) > MSG_MAX_LEN) { - cout << ParseErrorCode(ERR_MSG_LEN_INVALID) << endl; + cout << ErrorCode2Str(ERR_MSG_LEN_INVALID) << endl; return RET_FAIL; } for (iter = 0; iter < logTypeNum; iter++) { - pLogClearMsg->logType = GetLogType(vecLogType[iter]); - if (pLogClearMsg->logType == 0xffff) { - cout << ParseErrorCode(ERR_LOG_TYPE_INVALID) << endl; - return RET_FAIL; - } + pLogClearMsg->logType = logType & (~ (logType - 1)); //Get first bit 1 - logType + logType &= (~pLogClearMsg->logType); pLogClearMsg++; } SetMsgHead(&pLogClearReq->msgHeader, msgCmd, sizeof(LogClearMsg) * logTypeNum); @@ -387,59 +268,45 @@ int32_t LogClearOp(SeqPacketSocketClient& controller, uint8_t msgCmd, const std: int32_t LogPersistOp(SeqPacketSocketClient& controller, uint8_t msgCmd, LogPersistParam* logPersistParam) { char msgToSend[MSG_MAX_LEN] = {0}; - vector vecLogType; - vector vecJobId; - uint32_t logTypeNum; - uint32_t jobIdNum; - uint32_t iter; - int ret = 0; - uint32_t fileSizeDefault = LOG_PERSIST_FILE_SIZE; - uint32_t fileNumDefault = LOG_PERSIST_FILE_NUM; - string logType = SetDefaultLogType(logPersistParam->logTypeStr); - Split(logType, " ", vecLogType); - Split(logPersistParam->jobIdStr, " ", vecJobId); - logTypeNum = vecLogType.size(); - jobIdNum = vecJobId.size(); + uint16_t logType = Str2ComboLogType(logPersistParam->logTypeStr); + if (LOG_TYPE_MAX == logType) { + cout << ErrorCode2Str(ERR_LOG_TYPE_INVALID) << endl; + return RET_FAIL; + } + uint32_t jobId; switch (msgCmd) { case MC_REQ_LOG_PERSIST_START: { LogPersistStartRequest* pLogPersistStartReq = reinterpret_cast(msgToSend); LogPersistStartMsg* pLogPersistStartMsg = reinterpret_cast(&pLogPersistStartReq->logPersistStartMsg); if (sizeof(LogPersistStartRequest) > MSG_MAX_LEN) { - cout << ParseErrorCode(ERR_MSG_LEN_INVALID) << endl; + cout << ErrorCode2Str(ERR_MSG_LEN_INVALID) << endl; return RET_FAIL; } - for (iter = 0; iter < logTypeNum; iter++) { - uint16_t tmpType = GetLogType(vecLogType[iter]); - if (tmpType == 0xffff) { - cout << ParseErrorCode(ERR_LOG_TYPE_INVALID) << endl; - return RET_FAIL; - } - pLogPersistStartMsg->logType = (0b01 << tmpType) | pLogPersistStartMsg->logType; - } - if (pLogPersistStartMsg->logType == (0b01 << LOG_KMSG)) { - pLogPersistStartMsg->jobId = (logPersistParam->jobIdStr == "") ? DEFAULT_KMSG_JOBID - : stoi(logPersistParam->jobIdStr); + pLogPersistStartMsg->logType = logType; + if (logPersistParam->jobIdStr == "") { + jobId = (logType == (0b01 << LOG_KMSG)) ? DEFAULT_KMSG_JOBID : DEFAULT_JOBID; } else { - pLogPersistStartMsg->jobId = (logPersistParam->jobIdStr == "") ? DEFAULT_JOBID - : stoi(logPersistParam->jobIdStr); + jobId = stoi(logPersistParam->jobIdStr); } - if (pLogPersistStartMsg->jobId <= 0) { - cout << ParseErrorCode(ERR_LOG_PERSIST_JOBID_INVALID) << endl; + if (jobId == 0) { + cout << ErrorCode2Str(ERR_LOG_PERSIST_JOBID_INVALID) << endl; return RET_FAIL; } - pLogPersistStartMsg->compressAlg = (logPersistParam->compressAlgStr == "") ? COMPRESS_TYPE_ZLIB : - GetCompressAlg(logPersistParam->compressAlgStr); - pLogPersistStartMsg->fileSize = (logPersistParam->fileSizeStr == "") ? fileSizeDefault : GetBuffSize( - logPersistParam->fileSizeStr); - pLogPersistStartMsg->fileNum = (logPersistParam->fileNumStr == "") ? fileNumDefault + pLogPersistStartMsg->jobId = jobId; + pLogPersistStartMsg->compressAlg = (logPersistParam->compressAlgStr == "") ? COMPRESS_TYPE_ZLIB + : Str2CompressType(logPersistParam->compressAlgStr); + pLogPersistStartMsg->fileSize = (logPersistParam->fileSizeStr == "") ? LOG_PERSIST_FILE_SIZE + : Str2Size(logPersistParam->fileSizeStr); + pLogPersistStartMsg->fileNum = (logPersistParam->fileNumStr == "") ? LOG_PERSIST_FILE_NUM : stoi(logPersistParam->fileNumStr); if (logPersistParam->fileNameStr.size() > FILE_PATH_MAX_LEN) { - cout << ParseErrorCode(ERR_LOG_PERSIST_FILE_NAME_INVALID) << endl; + cout << ErrorCode2Str(ERR_LOG_PERSIST_FILE_NAME_INVALID) << endl; return RET_FAIL; } - if (logPersistParam->fileNameStr != " ") { - ret += strcpy_s(pLogPersistStartMsg->filePath, FILE_PATH_MAX_LEN, logPersistParam->fileNameStr.c_str()); + if (logPersistParam->fileNameStr != "" + && (strcpy_s(pLogPersistStartMsg->filePath, FILE_PATH_MAX_LEN, logPersistParam->fileNameStr.c_str()) != 0)) { + return RET_FAIL; } SetMsgHead(&pLogPersistStartReq->msgHeader, msgCmd, sizeof(LogPersistStartRequest)); controller.WriteAll(msgToSend, sizeof(LogPersistStartRequest)); @@ -452,21 +319,17 @@ int32_t LogPersistOp(SeqPacketSocketClient& controller, uint8_t msgCmd, LogPersi LogPersistStopMsg* pLogPersistStopMsg = reinterpret_cast(&pLogPersistStopReq->logPersistStopMsg); if (logPersistParam->jobIdStr == "") { - pLogPersistStopMsg->jobId = JOB_ID_ALL; - SetMsgHead(&pLogPersistStopReq->msgHeader, msgCmd, sizeof(LogPersistStopMsg)); - controller.WriteAll(msgToSend, sizeof(LogPersistStopMsg) + sizeof(MessageHeader)); - break; + jobId = JOB_ID_ALL; + } else { + jobId = stoi(logPersistParam->jobIdStr); } - if (jobIdNum * sizeof(LogPersistStopMsg) + sizeof(MessageHeader) > MSG_MAX_LEN) { - cout << ParseErrorCode(ERR_MSG_LEN_INVALID) << endl; + if (jobId == 0) { + cout << ErrorCode2Str(ERR_LOG_PERSIST_JOBID_INVALID) << endl; return RET_FAIL; } - for (iter = 0; iter < jobIdNum; iter++) { - pLogPersistStopMsg->jobId = stoi(vecJobId[iter]); - pLogPersistStopMsg++; - } - SetMsgHead(&pLogPersistStopReq->msgHeader, msgCmd, sizeof(LogPersistStopMsg) * jobIdNum); - controller.WriteAll(msgToSend, sizeof(LogPersistStopMsg) * jobIdNum + sizeof(MessageHeader)); + pLogPersistStopMsg->jobId = jobId; + SetMsgHead(&pLogPersistStopReq->msgHeader, msgCmd, sizeof(LogPersistStopMsg)); + controller.WriteAll(msgToSend, sizeof(LogPersistStopMsg) + sizeof(MessageHeader)); break; } @@ -476,14 +339,7 @@ int32_t LogPersistOp(SeqPacketSocketClient& controller, uint8_t msgCmd, LogPersi LogPersistQueryMsg* pLogPersistQueryMsg = reinterpret_cast(&pLogPersistQueryReq->logPersistQueryMsg); - for (iter = 0; iter < logTypeNum; iter++) { - uint16_t tmpType = GetLogType(vecLogType[iter]); - if (tmpType == 0xffff) { - cout << ParseErrorCode(ERR_LOG_TYPE_INVALID) << endl; - return RET_FAIL; - } - pLogPersistQueryMsg->logType = (0b01 << tmpType) | pLogPersistQueryMsg->logType; - } + pLogPersistQueryMsg->logType = logType; SetMsgHead(&pLogPersistQueryReq->msgHeader, msgCmd, sizeof(LogPersistQueryMsg)); controller.WriteAll(msgToSend, sizeof(LogPersistQueryRequest)); break; @@ -493,10 +349,6 @@ int32_t LogPersistOp(SeqPacketSocketClient& controller, uint8_t msgCmd, LogPersi break; } - if (ret) { - return RET_FAIL; - } - return RET_SUCCESS; } @@ -504,92 +356,78 @@ int32_t SetPropertiesOp(SeqPacketSocketClient& controller, uint8_t operationType { vector vecDomain; vector vecTag; - uint32_t domainNum, tagNum; - uint32_t iter; - string key, value; - Split(propertyParm->domainStr, " ", vecDomain); - Split(propertyParm->tagStr, " ", vecTag); + int domainNum, tagNum; + int ret = RET_SUCCESS; + Split(propertyParm->domainStr, DEFAULT_SPLIT_DELIMIT, vecDomain); + Split(propertyParm->tagStr, DEFAULT_SPLIT_DELIMIT, vecTag); domainNum = vecDomain.size(); tagNum = vecTag.size(); + LogLevel lvl; switch (operationType) { case OT_PRIVATE_SWITCH: - key = GetPropertyName(PROP_PRIVATE); if (propertyParm->privateSwitchStr == "on") { - PropertySet(key.c_str(), "true"); + ret = SetPrivateSwitchOn(true); cout << "hilog private formatter is enabled" << endl; } else if (propertyParm->privateSwitchStr == "off") { - PropertySet(key.c_str(), "false"); + ret = SetPrivateSwitchOn(false); cout << "hilog private formatter is disabled" << endl; } else { - cout << ParseErrorCode(ERR_PRIVATE_SWITCH_VALUE_INVALID) << endl; + cout << ErrorCode2Str(ERR_PRIVATE_SWITCH_VALUE_INVALID) << endl; return RET_FAIL; } break; case OT_KMSG_SWITCH: - key = GetPropertyName(PROP_KMSG); if (propertyParm->kmsgSwitchStr == "on") { - PropertySet(key.c_str(), "true"); + ret = SetKmsgSwitchOn(true); std::cout << "hilog will store kmsg log" << std::endl; } else if (propertyParm->kmsgSwitchStr == "off") { - PropertySet(key.c_str(), "false"); + ret = SetKmsgSwitchOn(false); std::cout << "hilog will not store kmsg log" << std::endl; } else { - std::cout << ParseErrorCode(ERR_KMSG_SWITCH_VALUE_INVALID) << std::endl; + std::cout << ErrorCode2Str(ERR_KMSG_SWITCH_VALUE_INVALID) << std::endl; return RET_FAIL; } break; case OT_LOG_LEVEL: - if (propertyParm->tagStr != "" && propertyParm->domainStr != "") { + lvl = (LogLevel)Str2LogLevel(propertyParm->logLevelStr); + if (lvl== 0) { return RET_FAIL; - } else if (propertyParm->domainStr != "") { // by domain - std::string keyPre = GetPropertyName(PROP_DOMAIN_LOG_LEVEL); - for (iter = 0; iter < domainNum; iter++) { - key = keyPre + vecDomain[iter]; - if (GetLogLevel(propertyParm->logLevelStr, value) == 0xffff) { - continue; - } - PropertySet(key.c_str(), value.c_str()); + } + if (propertyParm->domainStr != "") { // by domain + for (auto iter = 0; iter < domainNum; iter++) { + ret = SetDomainLevel(HexStr2Int(vecDomain[iter]), lvl); + if(ret != RET_SUCCESS) break; cout << "domain " << vecDomain[iter] << " level is set to " << propertyParm->logLevelStr << endl; } - } else if (propertyParm->tagStr != "") { // by tag - std::string keyPre = GetPropertyName(PROP_TAG_LOG_LEVEL); - for (iter = 0; iter < tagNum; iter++) { - key = keyPre + vecTag[iter]; - if (GetLogLevel(propertyParm->logLevelStr, value) == 0xffff) { - continue; - } - PropertySet(key.c_str(), value.c_str()); + } + if (propertyParm->tagStr != "") { // by tag + for (auto iter = 0; iter < tagNum; iter++) { + ret = SetTagLevel(vecTag[iter].c_str(), lvl); + if(ret != RET_SUCCESS) break; cout << "tag " << vecTag[iter] << " level is set to " << propertyParm->logLevelStr << endl; } - } else { - key = GetPropertyName(PROP_GLOBAL_LOG_LEVEL); - if (GetLogLevel(propertyParm->logLevelStr, value) == 0xffff) { - return RET_FAIL; - } - PropertySet(key.c_str(), value.c_str()); - cout << "global log level is set to " << propertyParm->logLevelStr << endl; + } + if (propertyParm->domainStr == "" && propertyParm->tagStr == "") { + ret = SetGlobalLevel(lvl); + cout << "global log level is set to " << propertyParm->logLevelStr << endl; } break; case OT_FLOW_SWITCH: if (propertyParm->flowSwitchStr == "pidon") { - key = GetPropertyName(PROP_PROCESS_FLOWCTRL); - PropertySet(key.c_str(), "true"); + ret = SetProcessSwitchOn(true); cout << "flow control by process is enabled" << endl; } else if (propertyParm->flowSwitchStr == "pidoff") { - key = GetPropertyName(PROP_PROCESS_FLOWCTRL); - PropertySet(key.c_str(), "false"); + ret = SetProcessSwitchOn(false); cout << "flow control by process is disabled" << endl; } else if (propertyParm->flowSwitchStr == "domainon") { - key = GetPropertyName(PROP_DOMAIN_FLOWCTRL); - PropertySet(key.c_str(), "true"); + ret = SetDomainSwitchOn(true); cout << "flow control by domain is enabled" << endl; } else if (propertyParm->flowSwitchStr == "domainoff") { - key = GetPropertyName(PROP_DOMAIN_FLOWCTRL); - PropertySet(key.c_str(), "false"); + ret = SetDomainSwitchOn(false); cout << "flow control by domain is disabled" << endl; } else { - cout << ParseErrorCode(ERR_FLOWCTRL_SWITCH_VALUE_INVALID) << endl; + cout << ErrorCode2Str(ERR_FLOWCTRL_SWITCH_VALUE_INVALID) << endl; return RET_FAIL; } break; @@ -597,36 +435,7 @@ int32_t SetPropertiesOp(SeqPacketSocketClient& controller, uint8_t operationType default: break; } - return RET_SUCCESS; -} - - -int MultiQuerySplit(const std::string& src, const char& delim, std::vector& vecSplit) -{ - int srcSize = src.length(); - int findPos = 0; - int getPos = 0; - vecSplit.clear(); - - while (getPos < srcSize) { - findPos = src.find(delim, findPos); - if (-1 == findPos) { - if (getPos < srcSize) { - vecSplit.push_back(src.substr(getPos, srcSize - getPos)); - return 0; - } - } else if (findPos == getPos) { - vecSplit.push_back(std::string("")); - } else { - vecSplit.push_back(src.substr(getPos, findPos - getPos)); - } - getPos = ++findPos; - if (getPos == srcSize) { - vecSplit.push_back(std::string("")); - return 0; - } - } - return 0; + return ret != RET_SUCCESS ? RET_FAIL : RET_SUCCESS; } } // namespace HiviewDFX } // namespace OHOS diff --git a/services/hilogtool/log_display.cpp b/services/hilogtool/log_display.cpp index 28844ff..789d6ed 100644 --- a/services/hilogtool/log_display.cpp +++ b/services/hilogtool/log_display.cpp @@ -18,159 +18,16 @@ #include #include #include -#include #include "hilog/log.h" #include "format.h" #include "log_controller.h" #include "log_display.h" +#include "log_utils.h" namespace OHOS { namespace HiviewDFX { using namespace std; -using hash_t = std::uint64_t; -unordered_map errorMsg -{ - {RET_FAIL, "Unexpected error"}, - {ERR_LOG_LEVEL_INVALID, "Invalid log level, the valid log levels include D/I/W/E/F"}, - {ERR_LOG_TYPE_INVALID, "Invalid log type, the valid log types include app/core/init/kmsg"}, - {ERR_QUERY_TYPE_INVALID, "Query condition on both types and excluded types is undefined or\ - queryTypes can not contain app/core/init and kmsg at the same time"}, - {ERR_QUERY_LEVEL_INVALID, "Query condition on both levels and excluded levels is undefined"}, - {ERR_QUERY_DOMAIN_INVALID, "Invalid domain format, a hexadecimal number is needed"}, - {ERR_QUERY_TAG_INVALID, "Query condition on both tags and excluded tags is undefined"}, - {ERR_QUERY_PID_INVALID, "Query condition on both pid and excluded pid is undefined"}, - {ERR_BUFF_SIZE_EXP, "Buffer resize exception"}, - {ERR_LOG_PERSIST_FILE_SIZE_INVALID, "Invalid log persist file size, file size should be not less than " - + to_string(MAX_PERSISTER_BUFFER_SIZE)}, - {ERR_LOG_PERSIST_FILE_NAME_INVALID, "Invalid log persist file name, file name should not contain [\\/:*?\"<>|]"}, - {ERR_LOG_PERSIST_COMPRESS_BUFFER_EXP, "Invalid Log persist compression buffer"}, - {ERR_LOG_PERSIST_FILE_PATH_INVALID, "Invalid persister file path or persister directory does not exist"}, - {ERR_LOG_PERSIST_COMPRESS_INIT_FAIL, "Log persist compression initialization failed"}, - {ERR_LOG_PERSIST_FILE_OPEN_FAIL, "Log persist open file failed"}, - {ERR_LOG_PERSIST_MMAP_FAIL, "Log persist mmap failed"}, - {ERR_LOG_PERSIST_JOBID_FAIL, "Log persist jobid not exist"}, - {ERR_LOG_PERSIST_TASK_FAIL, "Log persist task is existed"}, - {ERR_DOMAIN_INVALID, "Invalid domain, domain should not be more than 0 and less than " - + to_string(DOMAIN_MAX_SCOPE)}, - {ERR_MEM_ALLOC_FAIL, "Alloc memory failed"}, - {ERR_MSG_LEN_INVALID, "Invalid message length, message length should be not more than " - + to_string(MSG_MAX_LEN)}, - {ERR_PRIVATE_SWITCH_VALUE_INVALID, "Invalid private switch value, valid:on/off"}, - {ERR_FLOWCTRL_SWITCH_VALUE_INVALID, "Invalid flowcontrl switch value, valid:pidon/pidoff/domainon/domainoff"}, - {ERR_LOG_PERSIST_JOBID_INVALID, "Invalid jobid, jobid should be more than 0"}, - {ERR_LOG_CONTENT_NULL, "Log content NULL"}, - {ERR_COMMAND_NOT_FOUND, "Command not found"}, - {ERR_FORMAT_INVALID, "Invalid format parameter"}, - {ERR_BUFF_SIZE_INVALID, "Invalid buffer size, buffer size should be more than 0 and less than " - + to_string(MAX_BUFFER_SIZE)}, - {ERR_COMMAND_INVALID, "Invalid command, only one control command can be executed each time"}, - {ERR_KMSG_SWITCH_VALUE_INVALID, "Invalid kmsg switch value, valid:on/off"} -}; - -string ParseErrorCode(ErrorCode errorCode) -{ - if (errorMsg.count(errorCode) == 0) { - cout << "ERR_CODE not exist" << endl; - } - string errorMsgStr = "[ERR_CODE:" + to_string(errorCode) + "], " + errorMsg[errorCode]; - return errorMsgStr; -} - -hash_t Hash(char const *str) -{ - hash_t ret {BASIS}; - while (*str) { - ret ^= *str; - ret *= PRIME; - str++; - } - return ret; -} -constexpr hash_t HashCompileTime(char const *str, hash_t lastValue = BASIS) -{ - return *str ? HashCompileTime(str + 1, (*str ^ lastValue) * PRIME) : lastValue; -} -constexpr unsigned long long operator "" _hash(char const *p, size_t) -{ - return HashCompileTime(p); -} - -string GetLogTypeStr(uint16_t logType) -{ - string logTypeStr = "invalid"; - if (logType == LOG_INIT) { - logTypeStr = "init"; - } - if (logType == LOG_CORE) { - logTypeStr = "core"; - } - if (logType == LOG_APP) { - logTypeStr = "app"; - } - if (logType == LOG_KMSG) { - logTypeStr = "kmsg"; - } - return logTypeStr; -} - -string GetOrigType(uint16_t shiftType) -{ - string logType = ""; - if (((1 << LOG_INIT) & shiftType) != 0) { - logType += "init,"; - } - if (((1 << LOG_CORE) & shiftType) != 0) { - logType += "core,"; - } - if (((1 << LOG_APP) & shiftType) != 0) { - logType += "app,"; - } - if (((1 << LOG_KMSG) & shiftType) != 0) { - logType += "kmsg,"; - } - logType.erase(logType.end() - 1); - return logType; -} - -string GetPressAlgStr(uint16_t pressAlg) -{ - string pressAlgStr; - if (pressAlg == COMPRESS_TYPE_ZSTD) { - pressAlgStr = "zstd"; - } - if (pressAlg == COMPRESS_TYPE_ZLIB) { - pressAlgStr = "zlib"; - } - return pressAlgStr; -} - -string GetByteLenStr(uint64_t buffSize) -{ - string buffSizeStr; - if (buffSize < ONE_KB) { - buffSizeStr += to_string(buffSize); - buffSizeStr += "B"; - } else if (buffSize < ONE_MB) { - buffSize = buffSize / ONE_KB; - buffSizeStr += to_string(buffSize); - buffSizeStr += "K"; - } else if (buffSize < ONE_GB) { - buffSize = buffSize / ONE_MB; - buffSizeStr += to_string(buffSize); - buffSizeStr += "M"; - } else if (buffSize < ONE_TB) { - buffSize = buffSize / ONE_GB; - buffSizeStr += to_string(buffSize); - buffSizeStr += "G"; - } else { - buffSize = buffSize / ONE_TB; - buffSizeStr += to_string(buffSize); - buffSizeStr += "T"; - } - return buffSizeStr; -} - /* * print control command operation result */ @@ -190,14 +47,14 @@ int32_t ControlCmdResult(const char* message) BuffSizeResult* pBuffSizeRst = (BuffSizeResult*)&pBuffSizeRsp->buffSizeRst; while (pBuffSizeRst && resultLen < msgLen) { if (pBuffSizeRst->result < 0) { - outputStr += GetLogTypeStr(pBuffSizeRst->logType); + outputStr += LogType2Str(pBuffSizeRst->logType); outputStr += " buffer size fail\n"; - outputStr += ParseErrorCode((ErrorCode)pBuffSizeRst->result); + outputStr += ErrorCode2Str((ErrorCode)pBuffSizeRst->result); outputStr += "\n"; } else { - outputStr += GetLogTypeStr(pBuffSizeRst->logType); + outputStr += LogType2Str(pBuffSizeRst->logType); outputStr += " buffer size is "; - outputStr += GetByteLenStr(pBuffSizeRst->buffSize); + outputStr += Size2Str(pBuffSizeRst->buffSize); outputStr += "\n"; } pBuffSizeRst++; @@ -213,14 +70,14 @@ int32_t ControlCmdResult(const char* message) BuffResizeResult* pBuffResizeRst = (BuffResizeResult*)&pBuffResizeRsp->buffResizeRst; while (pBuffResizeRst && resultLen < msgLen) { if (pBuffResizeRst->result < 0) { - outputStr += GetLogTypeStr(pBuffResizeRst->logType); + outputStr += LogType2Str(pBuffResizeRst->logType); outputStr += " buffer resize fail\n"; - outputStr += ParseErrorCode((ErrorCode)pBuffResizeRst->result); + outputStr += ErrorCode2Str((ErrorCode)pBuffResizeRst->result); outputStr += "\n"; } else { - outputStr += GetLogTypeStr(pBuffResizeRst->logType); + outputStr += LogType2Str(pBuffResizeRst->logType); outputStr += " buffer size is "; - outputStr += GetByteLenStr(pBuffResizeRst->buffSize); + outputStr += Size2Str(pBuffResizeRst->buffSize); outputStr += "\n"; } pBuffResizeRst++; @@ -237,24 +94,24 @@ int32_t ControlCmdResult(const char* message) if (staInfoQueryRsp->domain != 0xffffffff) { logOrDomain = to_string(staInfoQueryRsp->domain); } else { - logOrDomain = GetLogTypeStr(staInfoQueryRsp->logType); + logOrDomain = LogType2Str(staInfoQueryRsp->logType); } if (staInfoQueryRsp->result == RET_SUCCESS) { outputStr += logOrDomain; outputStr += " print log length is "; - outputStr += GetByteLenStr(staInfoQueryRsp->printLen); + outputStr += Size2Str(staInfoQueryRsp->printLen); outputStr += "\n"; outputStr += logOrDomain; outputStr += " cache log length is "; - outputStr += GetByteLenStr(staInfoQueryRsp->cacheLen); + outputStr += Size2Str(staInfoQueryRsp->cacheLen); outputStr += "\n"; outputStr += logOrDomain; outputStr += " dropped log lines is "; - outputStr += GetByteLenStr(staInfoQueryRsp->dropped); + outputStr += Size2Str(staInfoQueryRsp->dropped); } else if (staInfoQueryRsp->result < 0) { outputStr += logOrDomain; outputStr += " statistic info query fail\n"; - outputStr += ParseErrorCode((ErrorCode)staInfoQueryRsp->result); + outputStr += ErrorCode2Str((ErrorCode)staInfoQueryRsp->result); } break; } @@ -267,7 +124,7 @@ int32_t ControlCmdResult(const char* message) if (staInfoClearRsp->domain != 0xffffffff) { logOrDomain = to_string(staInfoClearRsp->domain); } else { - logOrDomain = GetLogTypeStr(staInfoClearRsp->logType); + logOrDomain = LogType2Str(staInfoClearRsp->logType); } if (staInfoClearRsp->result == RET_SUCCESS) { outputStr += logOrDomain; @@ -275,7 +132,7 @@ int32_t ControlCmdResult(const char* message) } else if (staInfoClearRsp->result < 0) { outputStr += logOrDomain; outputStr += " statistic info clear fail\n"; - outputStr += ParseErrorCode((ErrorCode)staInfoClearRsp->result); + outputStr += ErrorCode2Str((ErrorCode)staInfoClearRsp->result); } break; } @@ -287,12 +144,12 @@ int32_t ControlCmdResult(const char* message) LogClearResult* pLogClearRst = (LogClearResult*)&pLogClearRsp->logClearRst; while (pLogClearRst && resultLen < msgLen) { if (pLogClearRst->result < 0) { - outputStr += GetLogTypeStr(pLogClearRst->logType); + outputStr += LogType2Str(pLogClearRst->logType); outputStr += " log clear fail\n"; - outputStr += ParseErrorCode((ErrorCode)pLogClearRst->result); + outputStr += ErrorCode2Str((ErrorCode)pLogClearRst->result); outputStr += "\n"; } else { - outputStr += GetLogTypeStr(pLogClearRst->logType); + outputStr += LogType2Str(pLogClearRst->logType); outputStr += " log clear success "; outputStr += "\n"; } @@ -313,7 +170,7 @@ int32_t ControlCmdResult(const char* message) outputStr += "Persist task [jobid:"; outputStr += to_string(pLogPersistStartRst->jobId); outputStr += "] start failed\n"; - outputStr += ParseErrorCode((ErrorCode)pLogPersistStartRst->result); + outputStr += ErrorCode2Str((ErrorCode)pLogPersistStartRst->result); } else { outputStr += "Persist task [jobid:"; outputStr += to_string(pLogPersistStartRst->jobId); @@ -335,7 +192,7 @@ int32_t ControlCmdResult(const char* message) outputStr += "Persist task [jobid:"; outputStr += to_string(pLogPersistStopRst->jobId); outputStr += "] stop failed\n"; - outputStr += ParseErrorCode((ErrorCode)pLogPersistStopRst->result); + outputStr += ErrorCode2Str((ErrorCode)pLogPersistStopRst->result); } else { outputStr += "Persist task [jobid:"; outputStr += to_string(pLogPersistStopRst->jobId); @@ -356,19 +213,19 @@ int32_t ControlCmdResult(const char* message) while (pLogPersistQueryRst && resultLen < msgLen) { if (pLogPersistQueryRst->result < 0) { outputStr = "Persist task [logtype:"; - outputStr += GetLogTypeStr(pLogPersistQueryRst->logType); + outputStr += LogType2Str(pLogPersistQueryRst->logType); outputStr += "] query failed\n"; - outputStr += ParseErrorCode((ErrorCode)pLogPersistQueryRst->result); + outputStr += ErrorCode2Str((ErrorCode)pLogPersistQueryRst->result); } else { outputStr += to_string(pLogPersistQueryRst->jobId); outputStr += " "; - outputStr += GetOrigType(pLogPersistQueryRst->logType); + outputStr += ComboLogType2Str(pLogPersistQueryRst->logType); outputStr += " "; - outputStr += GetPressAlgStr(pLogPersistQueryRst->compressAlg); + outputStr += CompressType2Str(pLogPersistQueryRst->compressAlg); outputStr += " "; outputStr += pLogPersistQueryRst->filePath; outputStr += " "; - outputStr += to_string(pLogPersistQueryRst->fileSize); + outputStr += Size2Str(pLogPersistQueryRst->fileSize); outputStr += " "; outputStr += to_string(pLogPersistQueryRst->fileNum); outputStr += "\n"; @@ -385,45 +242,6 @@ int32_t ControlCmdResult(const char* message) return 0; } -HilogShowFormat HilogFormat (const char* formatArg) -{ - static HilogShowFormat format; - - switch (Hash(formatArg)) { - case "color"_hash: - format = COLOR_SHOWFORMAT; - break; - case "colour"_hash: - format = COLOR_SHOWFORMAT; - break; - case "time"_hash: - format = TIME_SHOWFORMAT; - break; - case "usec"_hash: - format = TIME_USEC_SHOWFORMAT; - break; - case "nsec"_hash: - format = TIME_NSEC_SHOWFORMAT; - break; - case "year"_hash: - format = YEAR_SHOWFORMAT; - break; - case "zone"_hash: - format = ZONE_SHOWFORMAT; - break; - case "epoch"_hash: - format = EPOCH_SHOWFORMAT; - break; - case "monotonic"_hash: - format = MONOTONIC_SHOWFORMAT; - break; - default: - cout << ParseErrorCode(ERR_FORMAT_INVALID) << endl; - exit(1); - } - return format; -} - /* * Match the logs according to the regular expression */ @@ -446,7 +264,7 @@ void HilogShowLog(uint32_t showFormat, HilogDataMessage* data, const HilogArgs* } if (data->length == 0) { - std::cout << ParseErrorCode(ERR_LOG_CONTENT_NULL) << endl; + std::cout << ErrorCode2Str(ERR_LOG_CONTENT_NULL) << endl; return; } diff --git a/services/hilogtool/log_utils.cpp b/services/hilogtool/log_utils.cpp new file mode 100644 index 0000000..b423035 --- /dev/null +++ b/services/hilogtool/log_utils.cpp @@ -0,0 +1,311 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include +#include + +#include +#include "log_utils.h" +#include "hilog_common.h" +#include "hilog_msg.h" +#include "hilogtool.h" + +namespace OHOS { +namespace HiviewDFX { +using namespace std; +template +class KVMap +{ +using ValueCmp = std::function; +public: + KVMap(std::unordered_map map, K def_k, V def_v) + : str_map(map), def_key(def_k), def_value(def_v) + { + compare = [](const V& v1, const V& v2){ return v1 == v2;}; + }; + + KVMap(std::unordered_map map, K def_k, V def_v, ValueCmp cmp) + : str_map(map), def_key(def_k), def_value(def_v), compare(cmp){}; + + const V& GetValue(K key) + { + auto it = str_map.find(key); + return it == str_map.end() ? def_value : it->second; + } + + const K GetKey(const V& value) + { + for (auto it = str_map.begin(); it == str_map.end(); it++) { + if (compare(value, it->second)) { + return it->first; + } + } + return def_key; + } +private: + const std::unordered_map str_map; + K def_key; + V def_value; + ValueCmp compare; +}; + +using StringMap = KVMap; + +//Error Codes&Strings Map +static StringMap g_ErrorMsgs( + { + {RET_FAIL, "Unexpected error"}, + {ERR_LOG_LEVEL_INVALID, "Invalid log level, the valid log levels include D/I/W/E/F"}, + {ERR_LOG_TYPE_INVALID, "Invalid log type, the valid log types include app/core/init/kmsg"}, + {ERR_QUERY_TYPE_INVALID, "Query condition on both types and excluded types is undefined or\ + queryTypes can not contain app/core/init and kmsg at the same time"}, + {ERR_QUERY_LEVEL_INVALID, "Query condition on both levels and excluded levels is undefined"}, + {ERR_QUERY_DOMAIN_INVALID, "Invalid domain format, a hexadecimal number is needed"}, + {ERR_QUERY_TAG_INVALID, "Query condition on both tags and excluded tags is undefined"}, + {ERR_QUERY_PID_INVALID, "Query condition on both pid and excluded pid is undefined"}, + {ERR_BUFF_SIZE_EXP, "Buffer resize exception"}, + {ERR_LOG_PERSIST_FILE_SIZE_INVALID, "Invalid log persist file size, file size should be not less than " + + to_string(MAX_PERSISTER_BUFFER_SIZE)}, + {ERR_LOG_PERSIST_FILE_NAME_INVALID, "Invalid log persist file name, file name should not contain [\\/:*?\"<>|]"}, + {ERR_LOG_PERSIST_COMPRESS_BUFFER_EXP, "Invalid Log persist compression buffer"}, + {ERR_LOG_PERSIST_FILE_PATH_INVALID, "Invalid persister file path or persister directory does not exist"}, + {ERR_LOG_PERSIST_COMPRESS_INIT_FAIL, "Log persist compression initialization failed"}, + {ERR_LOG_PERSIST_FILE_OPEN_FAIL, "Log persist open file failed"}, + {ERR_LOG_PERSIST_MMAP_FAIL, "Log persist mmap failed"}, + {ERR_LOG_PERSIST_JOBID_FAIL, "Log persist jobid not exist"}, + {ERR_LOG_PERSIST_TASK_FAIL, "Log persist task is existed"}, + {ERR_DOMAIN_INVALID, "Invalid domain, domain should not be more than 0 and less than " + + to_string(DOMAIN_MAX_SCOPE)}, + {ERR_MEM_ALLOC_FAIL, "Alloc memory failed"}, + {ERR_MSG_LEN_INVALID, "Invalid message length, message length should be not more than " + + to_string(MSG_MAX_LEN)}, + {ERR_PRIVATE_SWITCH_VALUE_INVALID, "Invalid private switch value, valid:on/off"}, + {ERR_FLOWCTRL_SWITCH_VALUE_INVALID, "Invalid flowcontrl switch value, valid:pidon/pidoff/domainon/domainoff"}, + {ERR_LOG_PERSIST_JOBID_INVALID, "Invalid jobid, jobid should be more than 0"}, + {ERR_LOG_CONTENT_NULL, "Log content NULL"}, + {ERR_COMMAND_NOT_FOUND, "Command not found"}, + {ERR_FORMAT_INVALID, "Invalid format parameter"}, + {ERR_BUFF_SIZE_INVALID, "Invalid buffer size, buffer size should be more than 0 and less than " + + to_string(MAX_BUFFER_SIZE)}, + {ERR_COMMAND_INVALID, "Invalid command, only one control command can be executed each time"}, + {ERR_KMSG_SWITCH_VALUE_INVALID, "Invalid kmsg switch value, valid:on/off"} + }, RET_FAIL, "Unknown error code" +); + +string ErrorCode2Str(uint16_t errorCode) +{ + return "[ERR_CODE: " + to_string(errorCode) + "] " + g_ErrorMsgs.GetValue((uint16_t)errorCode); +} + +//Log Types&Strings Map +static StringMap g_LogTypes( + { + {LOG_INIT, "init"}, {LOG_CORE, "core"}, {LOG_APP, "app"}, {LOG_KMSG, "kmsg"} + }, LOG_TYPE_MAX, "invalid" +); + +string LogType2Str(uint16_t logType) +{ + return g_LogTypes.GetValue(logType); +} + +uint16_t Str2LogType(const string& str) +{ + return g_LogTypes.GetKey(str); +} + +string ComboLogType2Str(uint16_t shiftType) +{ + LogType types[] = {LOG_INIT, LOG_CORE, LOG_APP, LOG_KMSG}; + string str=""; + uint16_t typeAll = 0; + + for (LogType t : types) { + typeAll |= (1 << t); + } + shiftType &= typeAll; + for (LogType t: types) { + if ((1 << t) & shiftType) { + str += LogType2Str(t); + shiftType &= (~(1 << t)); + } + if (shiftType != 0) { + str +","; + } else { + break; + } + } + return str; +} + +uint16_t Str2ComboLogType(const string& str) +{ + uint16_t logTypes = 0; + if (str == "") { + logTypes = (1 << LOG_CORE) | (1 << LOG_APP); + return logTypes; + } + vector vec; + Split(str, DEFAULT_SPLIT_DELIMIT, vec); + for (auto it : vec) { + uint16_t t = Str2LogType(it); + if (t == LOG_TYPE_MAX) { + return LOG_TYPE_MAX; + } + logTypes |= (1 << t); + } + return logTypes; +} + +//Log Levels&Strings Map +static StringMap g_LogLevels( + { + {LOG_DEBUG, "debug"}, {LOG_INFO, "info"}, {LOG_WARN, "warn"}, {LOG_ERROR, "error"}, + {LOG_FATAL, "fatal"} + }, 0, "invalid", [](const string& l1, const string& l2){ + if (l1.length() == 1 && std::tolower(l1[0]) == std::tolower(l2[0])) { + return true; + } else if (l1.length() == l2.length()) { + return std::equal(l1.begin(), l1.end(), l2.begin(), + [](char a, char b) { + return std::tolower(a) == std::tolower(b); + }); + } else { + return false; + } + } +); + +string LogLevel2Str(uint16_t logLevel) +{ + return g_LogLevels.GetValue(logLevel); +} + +uint16_t Str2LogLevel(const string& str) +{ + return g_LogLevels.GetKey(str); +} + +//Compress Types&Strings Map +static StringMap g_CompressTypes( + { + {COMPRESS_TYPE_NONE, "none"}, {COMPRESS_TYPE_ZLIB, "zlib"}, {COMPRESS_TYPE_ZSTD, "zstd"} + }, COMPRESS_TYPE_ZLIB, "unknown" +); + +string CompressType2Str(uint16_t compressType) +{ + return g_CompressTypes.GetValue(compressType); +} + +uint16_t Str2CompressType(const string& str) +{ + return g_CompressTypes.GetKey(str); +} + +//Showformat Types&Strings Map +static StringMap g_ShowFormats( + { + {OFF_SHOWFORMAT, "off"}, {COLOR_SHOWFORMAT, "color"}, {COLOR_SHOWFORMAT, "colour"}, + {TIME_SHOWFORMAT, "time"}, {TIME_USEC_SHOWFORMAT, "usec"}, {YEAR_SHOWFORMAT, "year"}, + {ZONE_SHOWFORMAT, "zone"}, {EPOCH_SHOWFORMAT, "epoch"}, {MONOTONIC_SHOWFORMAT, "monotonic"}, + {TIME_NSEC_SHOWFORMAT, "nsec"} + }, OFF_SHOWFORMAT, "invalid" +); + +string ShowFormat2Str(uint16_t showFormat) +{ + return g_ShowFormats.GetValue(showFormat); +} + +uint16_t Str2ShowFormat(const string& str) +{ + return g_ShowFormats.GetKey(str); +} + +//Buffer Size&Char Map +static KVMap g_SizeMap( + { + {'B', 1}, {'b', 1}, {'K', ONE_KB}, {'k', ONE_KB}, {'M', ONE_MB}, {'m', ONE_MB}, + {'G', ONE_GB}, {'g', ONE_GB}, {'T', ONE_TB}, {'t', ONE_TB} + }, ' ', 0 +); + +string Size2Str(uint64_t size) +{ + uint64_t unit = 1; + switch(size){ + case 0 ... ONE_KB - 1: unit = 1; break; + case ONE_KB ... ONE_MB - 1: unit = ONE_KB; break; + case ONE_MB ... ONE_GB - 1: unit = ONE_MB; break; + case ONE_GB ... ONE_TB - 1: unit = ONE_GB; break; + default: unit = ONE_TB; break; + } + return to_string(((float)size)/unit) + g_SizeMap.GetKey(unit); +} + +uint64_t Str2Size(const string& str) +{ + std::regex reg("[0-9]+[bBkKmMgGtT]?"); + if (!std::regex_match(str, reg)) { + return 0; + } + uint64_t index = str.size() - 1; + uint64_t unit = g_SizeMap.GetValue(str[index]); + + uint64_t value = stoull(str.substr(0, unit !=0 ? index : index + 1)); + return value * (unit != 0 ? unit : 1); +} + +//String split methods +void Split(const std::string& src, const std::string& separator, std::vector& dest) +{ + string str = src; + string substring; + string::size_type start = 0; + string::size_type index; + dest.clear(); + index = str.find_first_of(separator, start); + if (index == string::npos) { + dest.push_back(str); + return; + } + do { + substring = str.substr(start, index - start); + dest.push_back(substring); + start = index + separator.size(); + index = str.find(separator, start); + if (start == string::npos) { + break; + } + } while (index != string::npos); + substring = str.substr(start); + dest.emplace_back(substring); +} + +//Get bits count +uint32_t GetBitsCount(uint64_t n) +{ + uint32_t count = 0; + while (n != 0) { + ++count; + n = n & (n-1); + } + return count; +} + +} // namespace HiviewDFX +} // namespace OHOS \ No newline at end of file diff --git a/services/hilogtool/main.cpp b/services/hilogtool/main.cpp index d35e920..08047fd 100644 --- a/services/hilogtool/main.cpp +++ b/services/hilogtool/main.cpp @@ -26,9 +26,10 @@ #include "hilog/log.h" #include "hilog_common.h" -#include "hilogtool_msg.h" +#include "hilog_msg.h" #include "log_controller.h" #include "log_display.h" +#include "log_utils.h" namespace OHOS { namespace HiviewDFX { @@ -132,7 +133,7 @@ static int GetTypes(HilogArgs context, const string& typesArgs, bool exclude = f } else if (typesArgs == "kmsg") { types |= 1< Date: Wed, 9 Feb 2022 16:35:15 +0800 Subject: [PATCH 2/5] move hilogd service to pre-init Signed-off-by: yaomanhai --- services/hilogd/main.cpp | 40 +++++++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/services/hilogd/main.cpp b/services/hilogd/main.cpp index be64a89..d457e05 100644 --- a/services/hilogd/main.cpp +++ b/services/hilogd/main.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include "cmd_executor.h" #include "flow_control_init.h" @@ -38,8 +39,10 @@ namespace OHOS { namespace HiviewDFX { using namespace std; +using namespace std::chrono; constexpr int HILOG_FILE_MASK = 0026; +constexpr int WAITING_DATA_MS = 5000; #ifdef DEBUG static int g_fd = -1; @@ -57,16 +60,37 @@ static void SigHandler(int sig) } } +static int WaitingDataMounted(int max) +{ + chrono::steady_clock::time_point start = chrono::steady_clock::now(); + chrono::milliseconds wait(max); + + while (true) { + struct stat st; + if (stat(HILOG_FILE_DIR, &st) != -1) { + cout << "waiting for " << HILOG_FILE_DIR << " successfully!" << endl; + return 0; + } + std::this_thread::sleep_for(10ms); + if ((chrono::steady_clock::now() - start) > wait) { + cerr << "waiting for " << HILOG_FILE_DIR << " failed!" << endl; + return -1; + } + } +} + int HilogdEntry() { HilogBuffer hilogBuffer; umask(HILOG_FILE_MASK); #ifdef DEBUG - int fd = open(HILOG_FILE_DIR"hilogd.txt", O_WRONLY | O_APPEND); - if (fd > 0) { - g_fd = dup2(fd, fileno(stdout)); - } else { - std::cout << "open file error:" << strerror(errno) << std::endl; + if (WaitingDataMounted(WAITING_DATA_MS) == 0) { + int fd = open(HILOG_FILE_DIR"hilogd.txt", O_WRONLY | O_APPEND); + if (fd > 0) { + g_fd = dup2(fd, fileno(stdout)); + } else { + std::cout << "open file error:" << strerror(errno) << std::endl; + } } #endif std::signal(SIGINT, SigHandler); @@ -103,7 +127,9 @@ int HilogdEntry() auto startupCheckTask = std::async(std::launch::async, [&hilogBuffer]() { prctl(PR_SET_NAME, "hilogd.pst_res"); - RestorePersistJobs(hilogBuffer); + if (WaitingDataMounted(WAITING_DATA_MS) == 0) { + RestorePersistJobs(hilogBuffer); + } }); auto kmsgTask = std::async(std::launch::async, [&hilogBuffer]() { LogKmsg logKmsg(hilogBuffer); @@ -121,4 +147,4 @@ int main() { OHOS::HiviewDFX::HilogdEntry(); return 0; -} +} \ No newline at end of file -- Gitee From 42e2b30d57ec291a4e5136db144bf580e7460105 Mon Sep 17 00:00:00 2001 From: yaomanhai Date: Thu, 10 Feb 2022 17:54:43 +0800 Subject: [PATCH 3/5] hilog bug fix Signed-off-by: yaomanhai --- adapter/properties.cpp | 51 ++++--------------------- services/hilogd/etc/hilog.para | 4 +- services/hilogtool/log_controller.cpp | 29 +++++++------- services/hilogtool/log_utils.cpp | 55 +++++++++++++++++++-------- 4 files changed, 64 insertions(+), 75 deletions(-) mode change 100755 => 100644 services/hilogd/etc/hilog.para diff --git a/adapter/properties.cpp b/adapter/properties.cpp index 2cbc86b..bd4e736 100644 --- a/adapter/properties.cpp +++ b/adapter/properties.cpp @@ -65,6 +65,8 @@ static pthread_mutex_t g_domainLevelLock = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t g_tagLevelLock = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t g_debugLock = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t g_persistDebugLock = PTHREAD_MUTEX_INITIALIZER; +static pthread_mutex_t g_kmsgLock = PTHREAD_MUTEX_INITIALIZER; +static pthread_mutex_t g_buffSizeLock = PTHREAD_MUTEX_INITIALIZER; //Only frequent using properties need cache vaule and then need lock static pthread_mutex_t *g_locks[] = { @@ -77,8 +79,8 @@ static pthread_mutex_t *g_locks[] = { &g_tagLevelLock, //PROP_TAG_LOG_LEVEL, &g_debugLock, // PROP_SINGLE_DEBUG, &g_persistDebugLock, // PROP_PERSIST_DEBUG, - nullptr, // PROP_KMSG, - nullptr, // PROP_BUFF_SIZE, + &g_kmsgLock, // PROP_KMSG, + &g_buffSizeLock, // PROP_BUFF_SIZE, nullptr, // PROP_MAX }; @@ -218,48 +220,6 @@ private: DataConverter m_converter; }; -using SwitchCache = CacheData; -using LogLevelCache = CacheData; - -/* -static void PropertyGet(const string &key, char *value, int len) -{ - if (len > HILOG_PROP_VALUE_MAX) { - std::cerr << "PropertyGet(): len exceed maximum.\n"; - return; - } - - auto handle = FindParameter(key.c_str()); - if (handle == -1) { - return; - } - - auto res = GetParameterValue(handle, value, len); - if (res < 0) { - std::cerr << "PropertyGet() -> GetParameterValue -> Can't get value for key: " << key; - std::cerr << " handle: " << handle << " Result: " << res << "\n"; - } -} - -static void PropertySet(const string &key, const char* value) -{ - auto len = value ? strlen(value) : 0; - if (len > HILOG_PROP_VALUE_MAX) { - std::cerr << "PropertySet(): len exceed maximum.\n"; - return; - } - - auto result = SetParameter(key.c_str(), value); - if (result < 0) { - if (result == EC_INVALID) { - std::cerr << "PropertySet(): Invalid arguments.\n"; - } else { - std::cerr << "PropertySet(): Error: " << result << "\n"; - } - } -} -*/ - string GetProgName() { #ifdef HILOG_USE_MUSL @@ -269,6 +229,9 @@ string GetProgName() #endif } +using SwitchCache = CacheData; +using LogLevelCache = CacheData; + static bool TextToBool(const RawPropertyData& data, bool defaultVal) { if (!strcmp(data.data(), "true")) { diff --git a/services/hilogd/etc/hilog.para b/services/hilogd/etc/hilog.para old mode 100755 new mode 100644 index 6d55603..0c10c4f --- a/services/hilogd/etc/hilog.para +++ b/services/hilogd/etc/hilog.para @@ -14,9 +14,9 @@ hilog.private.on=true hilog.debug.on=false -persist.sys.hilog.kmsg.on=false -persist.sys.hilog.debug.on=false hilog.flowctrl.pid.on=false hilog.flowctrl.domain.on=false +persist.sys.hilog.kmsg.on=false +persist.sys.hilog.debug.on=false hilog.loggable.global=d diff --git a/services/hilogtool/log_controller.cpp b/services/hilogtool/log_controller.cpp index 1db04c9..8c06e94 100644 --- a/services/hilogtool/log_controller.cpp +++ b/services/hilogtool/log_controller.cpp @@ -143,7 +143,7 @@ int32_t BufferSizeOp(SeqPacketSocketClient& controller, uint8_t msgCmd, const st { char msgToSend[MSG_MAX_LEN] = {0}; uint16_t logType = Str2ComboLogType(logTypeStr); - if (LOG_TYPE_MAX == logType) { + if (logType == 0) { cout << ErrorCode2Str(ERR_LOG_TYPE_INVALID) << endl; return RET_FAIL; } @@ -158,7 +158,8 @@ int32_t BufferSizeOp(SeqPacketSocketClient& controller, uint8_t msgCmd, const st } for (iter = 0; iter < logTypeNum; iter++) { pBuffSizeMsg->logType = logType & (~ (logType - 1)); //Get first bit 1 - logType - logType &= (~pBuffSizeMsg->logType); + logType &= (~(pBuffSizeMsg->logType)); + pBuffSizeMsg->logType = GetBitPos(pBuffSizeMsg->logType); pBuffSizeMsg++; } SetMsgHead(&pBuffSizeReq->msgHeader, msgCmd, sizeof(BuffSizeMsg) * logTypeNum); @@ -175,6 +176,7 @@ int32_t BufferSizeOp(SeqPacketSocketClient& controller, uint8_t msgCmd, const st for (iter = 0; iter < logTypeNum; iter++) { pBuffResizeMsg->logType = logType & (~ (logType - 1)); //Get first bit 1 - logType logType &= (~pBuffResizeMsg->logType); + pBuffResizeMsg->logType = GetBitPos(pBuffResizeMsg->logType); pBuffResizeMsg->buffSize = Str2Size(buffSizeStr); pBuffResizeMsg++; } @@ -195,22 +197,22 @@ int32_t StatisticInfoOp(SeqPacketSocketClient& controller, uint8_t msgCmd, if ((logTypeStr != "" && domainStr != "") || (logTypeStr == "" && domainStr == "")) { return RET_FAIL; } - uint16_t logType = Str2ComboLogType(logTypeStr); - uint32_t domain; + uint16_t logType = Str2LogType(logTypeStr); + uint32_t domain = 0; if (logType == LOG_TYPE_MAX) { cout << ErrorCode2Str(ERR_LOG_TYPE_INVALID) << endl; return RET_FAIL; } - if (domainStr == "") { - domain = 0xffffffff; - } else { + if (domainStr != "") { domain = HexStr2Int(domainStr); - } - if (domain == 0 || domain > DOMAIN_MAX_SCOPE) { - cout << ErrorCode2Str(ERR_DOMAIN_INVALID) << endl; - return RET_FAIL; + if (domain == 0 || domain > DOMAIN_MAX_SCOPE) { + cout << ErrorCode2Str(ERR_DOMAIN_INVALID) << endl; + return RET_FAIL; + } + } else { + domain = 0xffffffff; } switch (msgCmd) { case MC_REQ_STATISTIC_INFO_QUERY: { @@ -239,7 +241,7 @@ int32_t LogClearOp(SeqPacketSocketClient& controller, uint8_t msgCmd, const std: { char msgToSend[MSG_MAX_LEN] = {0}; uint16_t logType = Str2ComboLogType(logTypeStr); - if (LOG_TYPE_MAX == logType) { + if (logType == 0) { cout << ErrorCode2Str(ERR_LOG_TYPE_INVALID) << endl; return RET_FAIL; } @@ -258,6 +260,7 @@ int32_t LogClearOp(SeqPacketSocketClient& controller, uint8_t msgCmd, const std: for (iter = 0; iter < logTypeNum; iter++) { pLogClearMsg->logType = logType & (~ (logType - 1)); //Get first bit 1 - logType logType &= (~pLogClearMsg->logType); + pLogClearMsg->logType = GetBitPos(pLogClearMsg->logType); pLogClearMsg++; } SetMsgHead(&pLogClearReq->msgHeader, msgCmd, sizeof(LogClearMsg) * logTypeNum); @@ -269,7 +272,7 @@ int32_t LogPersistOp(SeqPacketSocketClient& controller, uint8_t msgCmd, LogPersi { char msgToSend[MSG_MAX_LEN] = {0}; uint16_t logType = Str2ComboLogType(logPersistParam->logTypeStr); - if (LOG_TYPE_MAX == logType) { + if (logType == 0) { cout << ErrorCode2Str(ERR_LOG_TYPE_INVALID) << endl; return RET_FAIL; } diff --git a/services/hilogtool/log_utils.cpp b/services/hilogtool/log_utils.cpp index b423035..c30f4a9 100644 --- a/services/hilogtool/log_utils.cpp +++ b/services/hilogtool/log_utils.cpp @@ -26,6 +26,7 @@ namespace OHOS { namespace HiviewDFX { using namespace std; + template class KVMap { @@ -48,13 +49,23 @@ public: const K GetKey(const V& value) { - for (auto it = str_map.begin(); it == str_map.end(); it++) { - if (compare(value, it->second)) { - return it->first; + for (auto& it : str_map) { + if (compare(value, it.second)) { + return it.first; } } return def_key; } + + vector GetAllKeys() + { + vector keys; + for (auto& it : str_map) { + keys.push_back(it.first); + } + return keys; + } + private: const std::unordered_map str_map; K def_key; @@ -62,10 +73,8 @@ private: ValueCmp compare; }; -using StringMap = KVMap; - //Error Codes&Strings Map -static StringMap g_ErrorMsgs( +static KVMap g_ErrorMsgs( { {RET_FAIL, "Unexpected error"}, {ERR_LOG_LEVEL_INVALID, "Invalid log level, the valid log levels include D/I/W/E/F"}, @@ -105,11 +114,12 @@ static StringMap g_ErrorMsgs( }, RET_FAIL, "Unknown error code" ); -string ErrorCode2Str(uint16_t errorCode) +string ErrorCode2Str(int16_t errorCode) { return "[ERR_CODE: " + to_string(errorCode) + "] " + g_ErrorMsgs.GetValue((uint16_t)errorCode); } +using StringMap = KVMap; //Log Types&Strings Map static StringMap g_LogTypes( { @@ -129,22 +139,20 @@ uint16_t Str2LogType(const string& str) string ComboLogType2Str(uint16_t shiftType) { - LogType types[] = {LOG_INIT, LOG_CORE, LOG_APP, LOG_KMSG}; + vector types = g_LogTypes.GetAllKeys(); string str=""; uint16_t typeAll = 0; - for (LogType t : types) { + for (uint16_t t : types) { typeAll |= (1 << t); } shiftType &= typeAll; - for (LogType t: types) { + for (uint16_t t: types) { if ((1 << t) & shiftType) { - str += LogType2Str(t); shiftType &= (~(1 << t)); + str += (LogType2Str(t) + (shiftType != 0 ? "," : "")); } - if (shiftType != 0) { - str +","; - } else { + if (shiftType == 0) { break; } } @@ -160,10 +168,13 @@ uint16_t Str2ComboLogType(const string& str) } vector vec; Split(str, DEFAULT_SPLIT_DELIMIT, vec); - for (auto it : vec) { + for (auto& it : vec) { + if (it == "") { + continue; + } uint16_t t = Str2LogType(it); if (t == LOG_TYPE_MAX) { - return LOG_TYPE_MAX; + continue; } logTypes |= (1 << t); } @@ -307,5 +318,17 @@ uint32_t GetBitsCount(uint64_t n) return count; } +//Get position of the bit set +uint16_t GetBitPos(uint64_t n) +{ + if (!(n && (!(n & (n-1))))) { // only accpet the number which is power of 2 + return 0; + } + + uint16_t i = 0; + while(n >> (i++)) {} + i--; + return i-1; +} } // namespace HiviewDFX } // namespace OHOS \ No newline at end of file -- Gitee From 9d4879995aaa80e4291dc46db22eefbc6f5e029d Mon Sep 17 00:00:00 2001 From: yaomanhai Date: Thu, 10 Feb 2022 19:34:19 +0800 Subject: [PATCH 4/5] hilog bug fix Signed-off-by: yaomanhai --- services/hilogtool/include/log_utils.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/services/hilogtool/include/log_utils.h b/services/hilogtool/include/log_utils.h index f4d5e0a..25fd3bd 100644 --- a/services/hilogtool/include/log_utils.h +++ b/services/hilogtool/include/log_utils.h @@ -22,7 +22,7 @@ namespace OHOS { namespace HiviewDFX { -std::string ErrorCode2Str(uint16_t errorCode); +std::string ErrorCode2Str(int16_t errorCode); std::string LogType2Str(uint16_t logType); uint16_t Str2LogType(const std::string& str); std::string ComboLogType2Str(uint16_t shiftType); @@ -39,6 +39,7 @@ uint64_t Str2Size(const std::string& str); constexpr char DEFAULT_SPLIT_DELIMIT[] = ","; void Split(const std::string& src, const std::string& separator, std::vector& dest); uint32_t GetBitsCount(uint64_t n); +uint16_t GetBitPos(uint64_t n); } // namespace HiviewDFX } // namespace OHOS -#endif // LOG_UTILS_H \ No newline at end of file +#endif // LOG_UTILS_H -- Gitee From 6a1036da1361d026a2ddd8aec9746296dde883d1 Mon Sep 17 00:00:00 2001 From: yaomanhai Date: Thu, 10 Feb 2022 19:35:35 +0800 Subject: [PATCH 5/5] move hilogd service to pre-init Signed-off-by: yaomanhai --- services/hilogd/etc/hilogd.cfg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/hilogd/etc/hilogd.cfg b/services/hilogd/etc/hilogd.cfg index 57dd54f..ea12b73 100755 --- a/services/hilogd/etc/hilogd.cfg +++ b/services/hilogd/etc/hilogd.cfg @@ -5,7 +5,7 @@ "write /proc/sys/net/unix/max_dgram_qlen 600" ] }, { - "name" : "post-fs-data", + "name" : "pre-init", "cmds" : [ "mkdir /data/log/ 0770 system log", "mkdir /data/log/hilog/ 0750 logd log", -- Gitee