From 733b36837f6a250e52d54dbfa8cbe02baec07689 Mon Sep 17 00:00:00 2001 From: matv864 Date: Fri, 10 Jan 2025 14:44:36 +1000 Subject: [PATCH] save --- task_3/CMakeLists.txt | 13 ++++---- task_3/log_writer.cpp | 66 +++++++++++++++++++++++++++++++++++-- task_3/log_writer.h | 12 +++++-- task_3/main.cpp | 39 +++++++++++++++++++--- task_3/process_launcher.cpp | 19 +++++++++++ task_3/process_launcher.h | 12 +++---- 6 files changed, 139 insertions(+), 22 deletions(-) diff --git a/task_3/CMakeLists.txt b/task_3/CMakeLists.txt index 8f1bc14..b327208 100644 --- a/task_3/CMakeLists.txt +++ b/task_3/CMakeLists.txt @@ -1,12 +1,11 @@ -cmake_minimum_required(VERSION 3.13) # Проверка версии CMake. - # Если версия установленной программы - # старее указаной, произойдёт аварийный выход. +cmake_minimum_required(VERSION 3.13) -project(hello-world-cpp-logger) # Название проекта +project(hello-world-cpp-logger) -add_library(log_writer STATIC log_writer.cpp) # Создание статической библиотеки с именем process_launcher +add_library(log_writer STATIC log_writer.cpp) +add_library(process_launcher STATIC process_launcher.cpp) -add_executable(main main.cpp) # Создает исполняемый файл с именем main +add_executable(main main.cpp) -target_link_libraries(main log_writer) # Линковка программы с библиотекой \ No newline at end of file +target_link_libraries(main log_writer process_launcher) \ No newline at end of file diff --git a/task_3/log_writer.cpp b/task_3/log_writer.cpp index 10b54b6..5cdc4e5 100644 --- a/task_3/log_writer.cpp +++ b/task_3/log_writer.cpp @@ -3,7 +3,7 @@ int LogWriter::write_log(const std::string& log_line) { std::string new_line = "\n"; int fd = open( - "log.txt", + "main.log", O_WRONLY|O_APPEND|O_CREAT ); if (fd == -1) { @@ -17,7 +17,67 @@ int LogWriter::write_log(const std::string& log_line) { } -int LogWriter::log_start_line(int pid){ - write_log("hello"); +std::string LogWriter::get_current_time() { + time_t current_time = std::time(nullptr); + return ctime(¤t_time); +} + + +int LogWriter::log_start_line(){ + std::string current_time = "00:00:00"; + std::string log_line = ( + "start programm with pid " + + std::to_string(getpid()) + + " at " + + current_time + ); + write_log(log_line); return 0; +} + + +int LogWriter::log_counter(int counter){ + std::string current_time = "00:00:00"; + std::string log_line = ( + "counter at " + + current_time + + " in process with pid " + + std::to_string(getpid()) + + " is " + + std::to_string(counter) + ); + write_log(log_line); + return 0; +} + + +int LogWriter::log_copy_start(int counter, std::string copy_name) { + std::string current_time = "00:00:00"; + std::string log_line = ( + copy_name + + " is started at " + + current_time + + " with pid " + + std::to_string(getpid()) + ); + write_log(log_line); + return 0; +} + +int LogWriter::log_copy_finish(std::string copy_name) { + std::string current_time = "00:00:00"; + std::string log_line = ( + copy_name + + " is finished at " + + current_time + ); + write_log(log_line); +} + +int LogWriter::log_copy_error(std::string copy_name) { + std::string log_line = ( + copy_name + + " is not finished" + ); + write_log(log_line); } \ No newline at end of file diff --git a/task_3/log_writer.h b/task_3/log_writer.h index 9428df5..1bf5532 100644 --- a/task_3/log_writer.h +++ b/task_3/log_writer.h @@ -3,13 +3,21 @@ #include #include +#include + class LogWriter { public: - static int log_start_line(int pid); + static int log_start_line(); + static int log_counter(int counter); + static int log_copy_start(int counter, std::string copy_name); + static int log_copy_finish(std::string copy_name); + static int log_copy_error(std::string copy_name); - +protected: + static std::string get_current_time(); private: static int write_log(const std::string& log_line); + }; diff --git a/task_3/main.cpp b/task_3/main.cpp index fdd0c9c..d34d737 100644 --- a/task_3/main.cpp +++ b/task_3/main.cpp @@ -1,6 +1,13 @@ +#include "process_launcher.h" #include "log_writer.h" +#include // for ms in timestamp +#include // for debugging -#include + +uint64_t get_timestamp_ms() { + using namespace std::chrono; + return duration_cast(system_clock::now().time_since_epoch()).count(); +} int main(int argc, char* argv[]) { if (argc >= 2) { @@ -15,8 +22,32 @@ int main(int argc, char* argv[]) { } } - std::cout << "launch main part\n"; - int res = LogWriter::log_start_line(1); - std::cout << res; + LogWriter::log_start_line(); + + long long int counter = 0; + uint64_t last_counter_increment = get_timestamp_ms(); + uint64_t last_counter_logging = get_timestamp_ms(); + uint64_t last_copies_launching = get_timestamp_ms(); + uint64_t current_time; + + while (1) { + current_time = get_timestamp_ms(); + + if ((current_time - last_counter_increment) >= 300) { + last_counter_increment = current_time; + counter++; + } + if ((current_time - last_counter_logging) >= 1000) { + last_counter_logging = current_time; + LogWriter::log_counter(counter); + } + if ((current_time - last_copies_launching) >= 3000) { + last_copies_launching = current_time; + } + + + + } + return 0; } \ No newline at end of file diff --git a/task_3/process_launcher.cpp b/task_3/process_launcher.cpp index 09bdaed..4c4dba3 100644 --- a/task_3/process_launcher.cpp +++ b/task_3/process_launcher.cpp @@ -32,6 +32,16 @@ int ProcessLauncher::waitForProcessWindows(HANDLE processHandle) { return -1; } +int ProcessLauncher::isProcessActive(int pid) { + return isProcessActiveWindows((HANDLE)pid); +} + +int ProcessLauncher::isProcessActiveWindows(HANDLE processHandle) { + DWORD ret = WaitForSingleObject(processHandle, 0); + bool isRunning = (ret == WAIT_TIMEOUT); + return isRunning; +} + #else // Для UNIX-подобных систем используем fork и exec @@ -73,4 +83,13 @@ int ProcessLauncher::waitForProcessUnix(pid_t pid) { return -1; } +int ProcessLauncher::isProcessActive(int pid) { + return isProcessActiveUnix(pid); +} + +int ProcessLauncher::isProcessActiveUnix(pid_t pid) { + // TODO + return -1; +} + #endif \ No newline at end of file diff --git a/task_3/process_launcher.h b/task_3/process_launcher.h index 701933f..1944de8 100644 --- a/task_3/process_launcher.h +++ b/task_3/process_launcher.h @@ -1,6 +1,3 @@ -#ifndef PROCESS_LAUNCHER_H -#define PROCESS_LAUNCHER_H - #ifdef _WIN32 #include #else @@ -19,16 +16,19 @@ public: // Ожидание завершения процесса и получение его кода возврата static int waitForProcess(int pid); + // проверка запущен ли процесс + static int isProcessActive(int pid); + private: #ifdef _WIN32 // Для Windows static int launchWindows(const std::string& command); static int waitForProcessWindows(HANDLE processHandle); + static int isProcessActiveWindows(HANDLE processHandle); #else // Для UNIX-подобных систем static int launchUnix(const std::string& command); static int waitForProcessUnix(pid_t pid); + static int isProcessActiveUnix(pid_t pid); #endif -}; - -#endif // PROCESS_LAUNCHER_H \ No newline at end of file +}; \ No newline at end of file