Skip to content

Commit

Permalink
Logging
Browse files Browse the repository at this point in the history
  • Loading branch information
cnbatch committed Dec 6, 2022
1 parent 64c5e2d commit ef1be5a
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 14 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ punchnat config1.conf config2.conf
```

### Log 文件
目前只提供输出 IP 地址到指定 Log 目录的功能。

在首次获取打洞后的 IP 地址与端口后,以及打洞的 IP 地址与端口发生变化后,会向 Log 目录创建 ip_address.txt 文件(若存在就追加),将 IP 地址与端口写进去。

获取到的打洞地址会同时显示在控制台当中。
Expand Down
12 changes: 6 additions & 6 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
add_subdirectory(shares)
add_subdirectory(networks)

target_link_libraries(${PROJECT_NAME} PRIVATE SHAREDEFINES)
target_link_libraries(${PROJECT_NAME} PRIVATE NETCONNECTIONS)

if (WIN32)
target_link_libraries(${PROJECT_NAME} PUBLIC wsock32 ws2_32)
endif()
if (UNIX)
target_link_libraries(${PROJECT_NAME} PUBLIC stdc++)
endif()

if(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
target_link_libraries(${PROJECT_NAME} PUBLIC uring)
endif()
#if(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
# target_link_libraries(${PROJECT_NAME} PUBLIC uring)
#endif()

if (${VCPKG_MANIFEST_DIR})
find_package(asio CONFIG REQUIRED)
Expand All @@ -19,9 +22,6 @@ else()
target_link_libraries(${PROJECT_NAME} PUBLIC Threads::Threads)
endif()

target_link_libraries(${PROJECT_NAME} PRIVATE SHAREDEFINES)
target_link_libraries(${PROJECT_NAME} PRIVATE NETCONNECTIONS)

add_compile_options("$<$<C_COMPILER_ID:MSVC>:/utf-8>")
add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/utf-8>")
set_property(TARGET ${PROJECT_NAME} PROPERTY
Expand Down
6 changes: 4 additions & 2 deletions src/networks/tcp_mode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ bool tcp_mode::start()
if (ec)
{
std::cerr << "TCP Mode - Listen Address incorrect - " << current_settings.listen_on << "\n";
if (!current_settings.log_messages.empty())
print_message_to_file("TCP Mode - Listen Address incorrect - " + current_settings.listen_on + "\n", current_settings.log_messages);
return false;
}

Expand Down Expand Up @@ -206,7 +208,7 @@ void tcp_mode::save_external_ip_address(uint32_t ipv4_address, uint16_t ipv4_por
ss << "TCP Mode - External IPv4 Port: " << ipv4_port << "\n";
std::string message = ss.str();
if (!current_settings.log_ip_address.empty())
asio::post(asio_strand, [message, log_ip_address = current_settings.log_ip_address]() { print_message_to_file(message, log_ip_address); });
print_ip_to_file(message, current_settings.log_ip_address);
std::cout << message;
}

Expand All @@ -223,7 +225,7 @@ void tcp_mode::save_external_ip_address(uint32_t ipv4_address, uint16_t ipv4_por
ss << "TCP Mode - External IPv6 Port: " << ipv6_port << "\n";
std::string message = ss.str();
if (!current_settings.log_ip_address.empty())
asio::post(asio_strand, [message, log_ip_address = current_settings.log_ip_address]() { print_message_to_file(message, log_ip_address); });
print_ip_to_file(message, current_settings.log_ip_address);
std::cout << message;
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/networks/udp_mode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ bool udp_mode::start()
if (ec)
{
std::cerr << "UDP Mode - Listen Address incorrect - " << current_settings.listen_on << "\n";
if (!current_settings.log_messages.empty())
print_message_to_file("UDP Mode - Listen Address incorrect - " + current_settings.listen_on + "\n", current_settings.log_messages);
return false;
}

Expand Down Expand Up @@ -238,7 +240,7 @@ void udp_mode::save_external_ip_address(uint32_t ipv4_address, uint16_t ipv4_por
ss << "UDP Mode - External IPv4 Port: " << ipv4_port << "\n";
std::string message = ss.str();
if (!current_settings.log_ip_address.empty())
asio::post(asio_strand, [message, log_ip_address = current_settings.log_ip_address]() { print_message_to_file(message, log_ip_address); });
print_ip_to_file(message, current_settings.log_ip_address);
std::cout << message;
}

Expand All @@ -255,7 +257,7 @@ void udp_mode::save_external_ip_address(uint32_t ipv4_address, uint16_t ipv4_por
ss << "UDP Mode - External IPv6 Port: " << ipv6_port << "\n";
std::string message = ss.str();
if (!current_settings.log_ip_address.empty())
asio::post(asio_strand, [message, log_ip_address = current_settings.log_ip_address]() { print_message_to_file(message, log_ip_address); });
print_ip_to_file(message, current_settings.log_ip_address);
std::cout << message;
}
}
19 changes: 17 additions & 2 deletions src/shares/share_defines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <stdexcept>
#include <cstdlib>
#include <fstream>
#include <mutex>
#include "share_defines.hpp"
#include "string_utils.hpp"

Expand Down Expand Up @@ -96,7 +97,10 @@ void check_settings(user_settings &current_user_settings, std::vector<std::strin
if (std::filesystem::exists(current_user_settings.log_directory))
{
if (std::filesystem::is_directory(current_user_settings.log_directory))
{
current_user_settings.log_ip_address = current_user_settings.log_directory / "ip_address.log";
current_user_settings.log_messages = current_user_settings.log_directory / "log_output.log";
}
else
error_msg.emplace_back("Log Path is not directory");
}
Expand All @@ -112,9 +116,20 @@ int64_t calculate_difference(int64_t number1, int64_t number2)
return std::abs(number1 - number2);
}

void print_message_to_file(const std::string &message, const std::filesystem::path &log_file)
void print_ip_to_file(const std::string& message, const std::filesystem::path& log_file)
{
std::ofstream output_file;
static std::ofstream output_file{};
static std::mutex mtx;
std::unique_lock locker{ mtx };
output_file.open(log_file, std::ios::out | std::ios::app);
output_file << message;
}

void print_message_to_file(const std::string& message, const std::filesystem::path& log_file)
{
static std::ofstream output_file{};
static std::mutex mtx;
std::unique_lock locker{ mtx };
output_file.open(log_file, std::ios::out | std::ios::app);
output_file << message;
}
2 changes: 2 additions & 0 deletions src/shares/share_defines.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ struct user_settings
std::string stun_server;
std::filesystem::path log_directory;
std::filesystem::path log_ip_address;
std::filesystem::path log_messages;
};

user_settings parse_from_args(const std::vector<std::string> &args, std::vector<std::string> &error_msg);
void check_settings(user_settings &current_user_settings, std::vector<std::string> &error_msg);
int64_t calculate_difference(int64_t number1, int64_t number2);
void print_ip_to_file(const std::string& message, const std::filesystem::path& log_file);
void print_message_to_file(const std::string &message, const std::filesystem::path &log_file);

#endif // !_SHARE_HEADER_

0 comments on commit ef1be5a

Please sign in to comment.