• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 
16 #ifdef OHOS_STANDARD_SYSTEM
17 #include "faultloggerd_client.h"
18 #endif
19 #include <cstring>
20 #include <iostream>
21 #include <fstream>
22 #include <sstream>
23 #include <atomic>
24 #include "dfx/log/ffrt_log_api.h"
25 #ifdef FFRT_SEND_EVENT
26 #include <securec.h>
27 #include "hisysevent.h"
28 #endif
29 #include "internal_inc/osal.h"
30 #include "util/white_list.h"
31 
32 static int g_ffrtLogLevel = FFRT_LOG_DEBUG;
33 static std::atomic<unsigned int> g_ffrtLogId(0);
34 static bool g_whiteListFlag = false;
35 namespace {
36     constexpr int LOG_BUFFER_SIZE = 2048;
37 }
38 
GetLogId(void)39 unsigned int GetLogId(void)
40 {
41     return ++g_ffrtLogId;
42 }
43 
IsInWhitelist(void)44 bool IsInWhitelist(void)
45 {
46     return g_whiteListFlag;
47 }
48 
GetFFRTLogLevel(void)49 int GetFFRTLogLevel(void)
50 {
51     return g_ffrtLogLevel;
52 }
53 
SetLogLevel(void)54 static void SetLogLevel(void)
55 {
56     std::string envLogStr = GetEnv("FFRT_LOG_LEVEL");
57     if (envLogStr.size() != 0) {
58         int level = std::stoi(envLogStr);
59         if (level < FFRT_LOG_LEVEL_MAX && level >= FFRT_LOG_ERROR) {
60             g_ffrtLogLevel = level;
61             return;
62         }
63     }
64 }
65 
InitWhiteListFlag(void)66 void InitWhiteListFlag(void)
67 {
68     g_whiteListFlag = ffrt::WhiteList::GetInstance().IsEnabled("log_ctr", true);
69 }
70 
LogInit(void)71 static __attribute__((constructor)) void LogInit(void)
72 {
73     SetLogLevel();
74     InitWhiteListFlag();
75 }
76 
77 #ifdef FFRT_SEND_EVENT
ReportSysEvent(const char * format,...)78 void ReportSysEvent(const char* format, ...)
79 {
80     char buffer[LOG_BUFFER_SIZE] = {0};
81     va_list args;
82     va_start(args, format);
83     int ret = vsnprintf_s(buffer, LOG_BUFFER_SIZE, LOG_BUFFER_SIZE - 1, format, args);
84     va_end(args);
85     if (ret < 0) {
86         return;
87     }
88     std::string msg = buffer;
89     HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::FFRT, "TASK_TIMEOUT",
90         OHOS::HiviewDFX::HiSysEvent::EventType::FAULT, "MSG", msg);
91 }
92 #endif // FFRT_SEND_EVENT