• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1  /*
2   * Copyright (c) 2022 Huawei Device Co., Ltd.
3   * Licensed under the Apache License, Version 2.0 (the "License");
4   * you may not use this file except in compliance with the License.
5   * You may obtain a copy of the License at
6   *
7   *     http://www.apache.org/licenses/LICENSE-2.0
8   *
9   * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  #ifndef FOUNDATION_RESOURCESCHEDULE_WORKSCHEDULER_UTILS_HILOG_H
16  #define FOUNDATION_RESOURCESCHEDULE_WORKSCHEDULER_UTILS_HILOG_H
17  
18  #include <string>
19  
20  #include "hilog/log.h"
21  
22  namespace OHOS {
23  namespace WorkScheduler {
24  #ifndef WORKSCHEDULER_DOMAIN_ID
25  #define WORKSCHEDULER_DOMAIN_ID 0xD001900
26  #endif
27  
28  #ifndef WORKSCHEDULER_MGR_LOG_TAG
29  #define WORKSCHEDULER_MGR_LOG_TAG "WORK_SCHEDULER"
30  #endif
31  
32  static constexpr OHOS::HiviewDFX::HiLogLabel WORKSCHEDULER_LABEL = {LOG_CORE,
33      WORKSCHEDULER_DOMAIN_ID, WORKSCHEDULER_MGR_LOG_TAG};
34  
35  enum class WorkSchedLogLevel : uint8_t { DEBUG = 0, INFO, WARN, ERROR, FATAL };
36  
37  class WorkSchedHilog {
38  public:
39      WorkSchedHilog() = delete;
40      ~WorkSchedHilog() = delete;
41  
42      static bool JudgeLevel(const WorkSchedLogLevel &level);
43  
SetLogLevel(const WorkSchedLogLevel & level)44      static void SetLogLevel(const WorkSchedLogLevel &level)
45      {
46          level_ = level;
47      }
48  
GetLogLevel()49      static const WorkSchedLogLevel &GetLogLevel()
50      {
51          return level_;
52      }
53  
54      static std::string GetBriefFileName(const char *str);
55  
56  private:
57      static WorkSchedLogLevel level_;
58  };
59  
60  #define WS_PRINT_LOG(LEVEL, Level, fmt, ...)                  \
61      if (WorkScheduler::WorkSchedHilog::JudgeLevel(WorkScheduler::WorkSchedLogLevel::LEVEL))     \
62      OHOS::HiviewDFX::HiLog::Level(WorkScheduler::WORKSCHEDULER_LABEL,               \
63          "[%{public}s(%{public}s):%{public}d] " fmt,        \
64          WorkScheduler::WorkSchedHilog::GetBriefFileName(__FILE__).c_str(), \
65          __FUNCTION__,                                      \
66          __LINE__,                                          \
67          ##__VA_ARGS__)
68  
69  #define WS_HILOGD(fmt, ...) WS_PRINT_LOG(DEBUG, Debug, fmt, ##__VA_ARGS__)
70  #define WS_HILOGI(fmt, ...) WS_PRINT_LOG(INFO, Info, fmt, ##__VA_ARGS__)
71  #define WS_HILOGW(fmt, ...) WS_PRINT_LOG(WARN, Warn, fmt, ##__VA_ARGS__)
72  #define WS_HILOGE(fmt, ...) WS_PRINT_LOG(ERROR, Error, fmt, ##__VA_ARGS__)
73  #define WS_HILOGF(fmt, ...) WS_PRINT_LOG(FATAL, Fatal, fmt, ##__VA_ARGS__)
74  } // namespace WorkScheduler
75  } // namespace OHOS
76  #endif // FOUNDATION_RESOURCESCHEDULE_WORKSCHEDULER_UTILS_HILOG_H