* Copyright (c) 2025 Huawei Technologies Co., Ltd.
* This program is free software, you can redistribute it and/or modify it under the terms and conditions of
* CANN Open Software License Agreement Version 2.0 (the "License").
* Please refer to the License for details. You may not use this file except in compliance with the License.
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
* See LICENSE in the root of the software repository for the full text of the License.
*/
#include "../pub_inc/trace.h"
#include <set>
#include "env_config.h"
namespace Hccl {
Trace::Trace()
{
}
bool Trace::isClosingChar(const char& c) const
{
static const std::set<char> closingChars = {
' ',')', ']', '}', ';', ',', '.', '!', '?',
};
return closingChars.find(c) != closingChars.end();
}
HcclResult Trace::Init(std::string &logInfo)
{
traceHandle = TraceCreate(logInfo.c_str());
if (traceHandle == TRACE_INVALID_HANDLE) {
HCCL_RUN_INFO("Trace::Init traceHandle is TRACE_INVALID_HANDLE");
}
return HcclResult::HCCL_SUCCESS;
}
void Trace::Save(std::string &buffer)
{
if (EnvConfig::GetInstance().GetLogConfig().GetEntryLogEnable()) {
HCCL_RUN_INFO("%s", buffer.c_str());
return;
}
u32 len = DEFAULT_ATRACE_MSG_SIZE - 1;
u32 pos = 0;
u32 totLen = 0;
u32 submitLen = 0;
u32 submitLenBak = 0;
totLen = buffer.length();
bool ret = true;
if (traceHandle == TRACE_INVALID_HANDLE) {
HCCL_WARNING("Tracve::Save Info = %s", buffer.c_str());
return;
}
char *startPos = static_cast<char *>(const_cast<char *>(buffer.c_str()));
while (pos < totLen) {
submitLen = (pos + len <= totLen) ? len : (totLen - pos);
submitLenBak = submitLen;
if (pos + len < totLen) {
while (submitLen > 0) {
if (isClosingChar(startPos[submitLen - 1])) {
break;
}
submitLen--;
}
if (submitLen == 0) {
submitLen = submitLenBak;
}
}
ret = ret && TraceSubmit(traceHandle, startPos, submitLen);
if (!ret) {
HCCL_WARNING("trace submit failed, ret[%d], traceInfo = [%s]", ret, startPos);
}
startPos += submitLen;
pos += submitLen;
}
if (!ret) {
HCCL_WARNING("trace submit failed, ret[%d], traceInfo = [%s]", ret, startPos);
}
}
Trace::~Trace()
{
TraceDestroy(traceHandle);
}
}