Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix windows compatibility #1741

Open
wants to merge 4 commits into
base: 2.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ elseif (MSVC)
set(CMAKE_STATIC_LINKER_FLAGS_RELEASE "${CMAKE_STATIC_LINKER_FLAGS_RELEASE} /DEBUG /OPT:REF /OPT:ICF")
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /DEBUG /OPT:REF /OPT:ICF")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd /MP")
if (MSVC_VERSION GREATER_EQUAL "1900")
add_compile_options("/std:c++17")
endif()
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
endif ()

# To be compatible with low version Linux.
Expand Down Expand Up @@ -95,6 +99,7 @@ if (LINUX)
elseif(MSVC)
if (ENABLE_ENTERPRISE)
set(SUB_DIRECTORIES_LIST ${SUB_DIRECTORIES_LIST} daemon)
set(SUB_DIRECTORIES_LIST ${SUB_DIRECTORIES_LIST} installer)
endif()
endif ()
if (ENABLE_ENTERPRISE)
Expand Down
4 changes: 4 additions & 0 deletions core/application/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@
#else
#include "config/provider/CommonConfigProvider.h"
#endif
#if defined(_MSC_VER)
#include "direct.h"
#include "common/WindowsDaemonUtil.h"
#endif

DEFINE_FLAG_BOOL(ilogtail_disable_core, "disable core in worker process", true);
DEFINE_FLAG_STRING(ilogtail_config_env_name, "config file path", "ALIYUN_LOGTAIL_CONFIG");
Expand Down
5 changes: 5 additions & 0 deletions core/checkpoint/AdhocCheckpointManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
#include "common/Thread.h"
#include "fuse/ulogfslib_file.h"
#include "common/HashUtil.h"
#if defined(_MSC_VER)
#include "windows.h"
#endif

DEFINE_FLAG_INT32(adhoc_checkpoint_dump_thread_wait_interval, "microseconds", 5 * 1000);

Expand Down Expand Up @@ -68,6 +71,7 @@ AdhocCheckpointManager::CreateAdhocJobCheckpoint(const std::string& jobName,
return jobCheckpoint;
}

#if !defined(_MSC_VER)
AdhocFileCheckpointPtr AdhocCheckpointManager::CreateAdhocFileCheckpoint(const std::string& jobName,
const std::string& filePath) {
fsutil::PathStat buf;
Expand Down Expand Up @@ -107,6 +111,7 @@ AdhocFileCheckpointPtr AdhocCheckpointManager::CreateAdhocFileCheckpoint(const s
return nullptr;
}
}
#endif

void AdhocCheckpointManager::DeleteAdhocJobCheckpoint(const std::string& jobName) {
auto jobCheckpoint = mAdhocJobCheckpointMap.find(jobName);
Expand Down
2 changes: 2 additions & 0 deletions core/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ elseif (LINUX)
# needed by observer
file(GLOB PICOHTTPPARSER_SOURCE_FILES protocol/picohttpparser/*.c protocol/picohttpparser/*.h)
list(APPEND LIB_SOURCE_FILES ${PICOHTTPPARSER_SOURCE_FILES})
list(REMOVE_ITEM LIB_SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/WindowsDaemonUtil.h)
list(REMOVE_ITEM LIB_SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/WindowsDaemonUtil.cpp)
endif ()
if (CMAKE_SYSTEM_PROCESSOR MATCHES "(x86)|(X86)|(amd64)|(AMD64)")
file(GLOB XX_HASH_SOURCE_FILES xxhash/*.c xxhash/*.h)
Expand Down
6 changes: 3 additions & 3 deletions core/common/EncodingConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ size_t EncodingConverter::ConvertGbk2Utf8(
return len;
}
char* des = desOut;
len = desLength - 1;
int len = desLength - 1;
des[len] = '\0';
int outLen = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR)wszUtf8, wcLen, des, len, NULL, NULL);
if (outLen) == 0) {
if ((outLen) == 0) {
LOG_ERROR(sLogger,
("convert GBK to UTF8 fail, WideCharToMultiByte error",
GetLastError())("sample", std::string(src, 0, 1024)));
Expand Down Expand Up @@ -179,7 +179,7 @@ std::string EncodingConverter::FromACPToUTF8(const std::string& s) const {

auto input = const_cast<char*>(s.c_str());
auto inputLen = s.length();
std::vector<size_t> ignore;
std::vector<long> ignore;

size_t outputLen = ConvertGbk2Utf8(input, &inputLen, nullptr, 0, ignore);
if (outputLen == 0) {
Expand Down
3 changes: 3 additions & 0 deletions core/common/MachineInfoUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#elif defined(_MSC_VER)
#include <WinSock2.h>
#include <Windows.h>
#include <ws2tcpip.h>
#endif
#include <curl/curl.h>
#include <rapidjson/stringbuffer.h>
Expand Down Expand Up @@ -168,6 +169,7 @@ std::string GetHostName() {
return std::string(hostname);
}

#if defined(__linux__)
std::unordered_set<std::string> GetNicIpv4IPSet() {
struct ifaddrs* ifAddrStruct = NULL;
void* tmpAddrPtr = NULL;
Expand All @@ -193,6 +195,7 @@ std::unordered_set<std::string> GetNicIpv4IPSet() {
freeifaddrs(ifAddrStruct);
return ipSet;
}
#endif

std::string GetHostIpByHostName() {
std::string hostname = GetHostName();
Expand Down
2 changes: 2 additions & 0 deletions core/common/RandomUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include <sstream>
#if defined(__linux__)
#include <uuid/uuid.h>
#elif defined(_MSC_VER)
#include "common/UUIDUtil.h"
#endif

namespace logtail {
Expand Down
93 changes: 93 additions & 0 deletions core/common/WindowsDaemonUtil.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright 2024 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 <vector>
#include "WindowsDaemonUtil.h"
#include "application/Application.h"
#include "common/Flags.h"
#include "common/RuntimeUtil.h"
#include "logger/Logger.h"
#include <windows.h>
using std::string;
using std::vector;
DEFINE_FLAG_INT32(win_open_global_objects_interval,
"interval to retry openning global objects in daemon, seconds",
60 * 30);
namespace logtail {
HANDLE g_DaemonMutex = NULL;
HANDLE g_DaemonEvent = NULL;
static bool OpenGlobalObjects(std::string& err) {
if (g_DaemonMutex != NULL && g_DaemonEvent != NULL)
return true;
auto binaryPath = logtail::GetBinaryName();
auto namePrefix = std::string("ILOGTAIL_HANDLE_PREFIX");
auto index = binaryPath.rfind("\\");
if (index > 0)
namePrefix = binaryPath.substr(0, index);
for (auto& c : namePrefix) {
if ('\\' == c)
c = '_';
}
g_DaemonMutex = OpenMutex(SYNCHRONIZE, FALSE, (namePrefix + "_mutex").c_str());
if (NULL == g_DaemonMutex) {
err = "Open daemon mutex failed, can not connect to daemon" + std::to_string(GetLastError());
return false;
}
g_DaemonEvent = OpenEvent(EVENT_ALL_ACCESS, FALSE, (namePrefix + "_event").c_str());
if (NULL == g_DaemonEvent) {
err = "Open daemon event failed, can not connect to daemon" + std::to_string(GetLastError());
CloseHandle(g_DaemonMutex);
g_DaemonMutex = NULL;
return false;
}
return true;
}
void InitWindowsSignalObject() {
// Open global named objects to communicate with daemon.
std::string errMsg;
if (!OpenGlobalObjects(errMsg)) {
LOG_ERROR(sLogger, ("Open global objects failed, disabled self update, error", errMsg));
}
}
void SyncWindowsSignalObject() {
// Check daemon mutex and assert heartbeat with event.
if (NULL == g_DaemonMutex) {
static auto lastOpenTime = time(0);
if (time(0) - lastOpenTime >= INT32_FLAG(win_open_global_objects_interval)) {
std::string err;
if (!OpenGlobalObjects(err)) {
LOG_ERROR(sLogger, ("Retry to open global objects failed", err));
} else {
LOG_INFO(sLogger, ("Retry to open global objects success, enable self update", ""));
}
lastOpenTime = time(0);
}
}
if (g_DaemonMutex != NULL) {
if (WAIT_OBJECT_0 == WaitForSingleObject(g_DaemonMutex, 0)) {
LOG_INFO(sLogger, ("Stop signal from daemon", ""));
Application::GetInstance()->SetSigTermSignalFlag(true);
LOG_INFO(sLogger, ("ReleaseMutex", ReleaseMutex(g_DaemonMutex)));
}
if (FALSE == SetEvent(g_DaemonEvent)) {
LOG_ERROR(sLogger, ("SetEvent error", GetLastError()));
}
}
}
void ReleaseWindowsSignalObject() {
if (g_DaemonMutex)
CloseHandle(g_DaemonMutex);
if (g_DaemonEvent)
CloseHandle(g_DaemonEvent);
}
} // namespace logtail
22 changes: 22 additions & 0 deletions core/common/WindowsDaemonUtil.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2024 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.
*/
#pragma once
// Windows daemon utility.
namespace logtail {
void InitWindowsSignalObject();
void SyncWindowsSignalObject();
void ReleaseWindowsSignalObject();
} // namespace logtail
2 changes: 1 addition & 1 deletion core/config/provider/CommonConfigProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ void CommonConfigProvider::Stop() {

void CommonConfigProvider::CheckUpdateThread() {
LOG_INFO(sLogger, ("common config provider", "started"));
usleep((rand() % 10) * 100 * 1000);
std::this_thread::sleep_for(std::chrono::seconds(1));
int32_t lastCheckTime = 0;
unique_lock<mutex> lock(mThreadRunningMux);
while (mIsThreadRunning) {
Expand Down
2 changes: 1 addition & 1 deletion core/config/provider/ConfigProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void ConfigProvider::Init(const string& dir) {

error_code ec;
filesystem::create_directories(mSourceDir, ec);
ConfigWatcher::GetInstance()->AddSource(mSourceDir, &mMux);
ConfigWatcher::GetInstance()->AddSource(mSourceDir.string(), &mMux);
}

} // namespace logtail
3 changes: 3 additions & 0 deletions core/file_server/FileDiscoveryOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
#include "common/LogtailCommonFlags.h"
#include "common/ParamExtractor.h"
#include "common/StringTools.h"
#if defined(_MSC_VER)
#include "EncodingConverter.h"
#endif

using namespace std;

Expand Down
9 changes: 6 additions & 3 deletions core/file_server/MultilineOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,15 +197,18 @@ bool MultilineOptions::ParseRegex(const string& pattern, shared_ptr<boost::regex
const std::string&
UnmatchedContentTreatmentToString(MultilineOptions::UnmatchedContentTreatment unmatchedContentTreatment) {
switch (unmatchedContentTreatment) {
case MultilineOptions::UnmatchedContentTreatment::DISCARD:
case MultilineOptions::UnmatchedContentTreatment::DISCARD: {
static std::string discardStr = "discard";
return discardStr;
case MultilineOptions::UnmatchedContentTreatment::SINGLE_LINE:
}
case MultilineOptions::UnmatchedContentTreatment::SINGLE_LINE: {
static std::string singleLine = "single line";
return singleLine;
default:
}
default: {
static std::string unkonwn = "";
return unkonwn;
}
}
}

Expand Down
1 change: 1 addition & 0 deletions core/logtail_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "common/Flags.h"
#include "logger/Logger.h"
#include "monitor/LogtailAlarm.h"
#include "common/version.h"

using namespace logtail;

Expand Down
12 changes: 8 additions & 4 deletions core/models/PipelineEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,22 @@ StringView gEmptyStringView;

const std::string& PipelineEventTypeToString(PipelineEvent::Type t) {
switch (t) {
case PipelineEvent::Type::LOG:
case PipelineEvent::Type::LOG: {
static std::string logname = "Log";
return logname;
case PipelineEvent::Type::METRIC:
}
case PipelineEvent::Type::METRIC: {
static std::string metricname = "Metric";
return metricname;
case PipelineEvent::Type::SPAN:
}
case PipelineEvent::Type::SPAN: {
static std::string spanname = "Span";
return spanname;
default:
}
default: {
static std::string voidname = "";
return voidname;
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions core/monitor/LogFileProfiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
#include "sender/Sender.h"
#include "config_manager/ConfigManager.h"
#include "app_config/AppConfig.h"
#if defined(_MSC_VER)
#include "EncodingConverter.h"
#endif

DEFINE_FLAG_INT32(profile_data_send_interval, "interval of send LogFile/DomainSocket profile data, seconds", 600);
DEFINE_FLAG_STRING(logtail_profile_snapshot, "reader profile on local disk", "logtail_profile_snapshot");
Expand Down
4 changes: 0 additions & 4 deletions core/plugin/PluginRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@

#include "plugin/PluginRegistry.h"

#include <dirent.h>
#include <dlfcn.h>
#include <unistd.h>

#include <cstdio>
#include <cstdlib>
#include <cstring>
Expand Down
Loading
Loading