1800b99b8Sopenharmony_ci/* 2800b99b8Sopenharmony_ci * Copyright (c) 2022-2024 Huawei Device Co., Ltd. 3800b99b8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4800b99b8Sopenharmony_ci * you may not use this file except in compliance with the License. 5800b99b8Sopenharmony_ci * You may obtain a copy of the License at 6800b99b8Sopenharmony_ci * 7800b99b8Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8800b99b8Sopenharmony_ci * 9800b99b8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10800b99b8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11800b99b8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12800b99b8Sopenharmony_ci * See the License for the specific language governing permissions and 13800b99b8Sopenharmony_ci * limitations under the License. 14800b99b8Sopenharmony_ci */ 15800b99b8Sopenharmony_ci#include "dfx_logger.h" 16800b99b8Sopenharmony_ci 17800b99b8Sopenharmony_ci#include <cstdio> 18800b99b8Sopenharmony_ci#include <securec.h> 19800b99b8Sopenharmony_ci#include <unistd.h> 20800b99b8Sopenharmony_ci#include "dfx_define.h" 21800b99b8Sopenharmony_ci#include "dfx_log.h" 22800b99b8Sopenharmony_ci#include "faultloggerd_client.h" 23800b99b8Sopenharmony_ci 24800b99b8Sopenharmony_cistatic const int WRITE_LOG_BUF_LEN = 2048; 25800b99b8Sopenharmony_ci 26800b99b8Sopenharmony_ciint WriteLog(int32_t fd, const char *format, ...) 27800b99b8Sopenharmony_ci{ 28800b99b8Sopenharmony_ci int ret = -1; 29800b99b8Sopenharmony_ci char buf[WRITE_LOG_BUF_LEN] = {0}; 30800b99b8Sopenharmony_ci va_list args; 31800b99b8Sopenharmony_ci va_start(args, format); 32800b99b8Sopenharmony_ci ret = vsnprintf_s(buf, sizeof(buf), sizeof(buf) - 1, format, args); 33800b99b8Sopenharmony_ci if (ret == -1) { 34800b99b8Sopenharmony_ci DFXLOGW("WriteLog: vsnprintf_s fail"); 35800b99b8Sopenharmony_ci } 36800b99b8Sopenharmony_ci va_end(args); 37800b99b8Sopenharmony_ci 38800b99b8Sopenharmony_ci if (fd >= 0) { 39800b99b8Sopenharmony_ci ret = dprintf(fd, "%s", buf); 40800b99b8Sopenharmony_ci if (ret < 0) { 41800b99b8Sopenharmony_ci DFXLOGE("WriteLog :: write msg(%{public}s) to fd(%{public}d) failed, ret(%{public}d).", buf, fd, ret); 42800b99b8Sopenharmony_ci } 43800b99b8Sopenharmony_ci } else if (fd == INVALID_FD) { 44800b99b8Sopenharmony_ci DFXLOGW("%{public}s", buf); 45800b99b8Sopenharmony_ci } else { 46800b99b8Sopenharmony_ci DFXLOGD("%{public}s", buf); 47800b99b8Sopenharmony_ci } 48800b99b8Sopenharmony_ci 49800b99b8Sopenharmony_ci return ret; 50800b99b8Sopenharmony_ci} 51800b99b8Sopenharmony_ci 52800b99b8Sopenharmony_civoid DfxLogToSocket(const char *msg) 53800b99b8Sopenharmony_ci{ 54800b99b8Sopenharmony_ci size_t length = strlen(msg); 55800b99b8Sopenharmony_ci if (length >= LOG_BUF_LEN) { 56800b99b8Sopenharmony_ci return; 57800b99b8Sopenharmony_ci } 58800b99b8Sopenharmony_ci int ret = RequestPrintTHilog(msg, length); 59800b99b8Sopenharmony_ci if (ret < 0) { 60800b99b8Sopenharmony_ci DFXLOGE("DfxLogToSocket :: request print msg(%{public}s) failed, ret(%{public}d).", msg, ret); 61800b99b8Sopenharmony_ci } 62800b99b8Sopenharmony_ci} 63