Ai
1 Star 0 Fork 1

SmartSmallBoy/loongcollector_cpp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
RuntimeUtil.cpp 3.81 KB
一键复制 编辑 原始数据 按行查看 历史
// Copyright 2022 iLogtail Authors
//
// 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 "RuntimeUtil.h"
#include "Flags.h"
#include "app_config/AppConfig.h"
#if defined(__linux__)
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#elif defined(_MSC_VER)
#include <Psapi.h>
#include <Windows.h>
#endif
#include <cstdio>
#include <errno.h>
#include <sstream>
#include "FileSystemUtil.h"
#include "LogtailCommonFlags.h"
#include "logger/Logger.h"
DECLARE_FLAG_STRING(logtail_sys_conf_dir);
DECLARE_FLAG_BOOL(logtail_mode);
namespace logtail {
// TODO: In ConfigManager.cpp, some places use / to concat path, which might fail on Windows,
// replace them with PATH_SEPARATOR.
std::string GetProcessExecutionDir(void) {
#if defined(__ANDROID__)
// In Android, runtime configuration files cannot be stored in the same directory as the executable
return STRING_FLAG(logtail_sys_conf_dir);
#elif defined(__linux__)
char exePath[PATH_MAX + 1] = "";
readlink("/proc/self/exe", exePath, sizeof(exePath));
std::string fullPath(exePath);
size_t index = fullPath.rfind(PATH_SEPARATOR);
if (index == std::string::npos) {
return "";
}
return fullPath.substr(0, index + 1);
#elif defined(_MSC_VER)
auto fullPath = GetBinaryName();
auto index = fullPath.rfind(PATH_SEPARATOR);
return (std::string::npos == index) ? "" : fullPath.substr(0, index + 1);
#endif
}
std::string GetBinaryName(void) {
#if defined(__linux__)
char exePath[PATH_MAX + 1] = "";
readlink("/proc/self/exe", exePath, sizeof(exePath));
std::string fullPath(exePath);
return fullPath;
#elif defined(_MSC_VER)
TCHAR filename[MAX_PATH];
if (0 == GetModuleFileName(NULL, filename, MAX_PATH)) {
LOG_ERROR(sLogger, ("GetModuleFileName failed", GetLastError()));
return "";
}
return std::string(filename);
#endif
}
// only loongcollector_config.json will be rebuild from memory
bool RebuildExecutionDir(const std::string& ilogtailConfigJson,
std::string& errorMessage,
const std::string& executionDir) {
if (BOOL_FLAG(logtail_mode)) {
std::string path = executionDir.empty() ? GetProcessExecutionDir() : executionDir;
if (CheckExistance(path))
return true;
if (!Mkdir(path)) {
std::stringstream ss;
ss << "create execution dir failed, errno is " << errno;
errorMessage = ss.str();
return false;
}
if (ilogtailConfigJson.empty())
return true;
FILE* pFile = fopen((path + STRING_FLAG(ilogtail_config)).c_str(), "w");
if (pFile == NULL) {
std::stringstream ss;
ss << "open " << STRING_FLAG(ilogtail_config) << " to write failed, errno is " << errno;
errorMessage = ss.str();
return false;
}
fwrite(ilogtailConfigJson.c_str(), 1, ilogtailConfigJson.size(), pFile);
fclose(pFile);
} else {
std::string path = GetAgentDataDir();
if (CheckExistance(path))
return true;
if (!Mkdirs(path)) {
std::stringstream ss;
ss << "create data dir failed, errno is " << errno;
errorMessage = ss.str();
return false;
}
}
return true;
}
} // namespace logtail
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/SmartSmallBoy/loongcollector_cpp.git
git@gitee.com:SmartSmallBoy/loongcollector_cpp.git
SmartSmallBoy
loongcollector_cpp
loongcollector_cpp
master

搜索帮助