This commit is contained in:
Ivanov Matvey 2025-01-10 14:44:36 +10:00
parent d5c0c20a9e
commit 733b36837f
6 changed files with 139 additions and 22 deletions

View File

@ -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) # Линковка программы с библиотекой
target_link_libraries(main log_writer process_launcher)

View File

@ -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(&current_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);
}

View File

@ -3,13 +3,21 @@
#include <fcntl.h>
#include <sys/stat.h>
#include <ctime>
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);
};

View File

@ -1,6 +1,13 @@
#include "process_launcher.h"
#include "log_writer.h"
#include <chrono> // for ms in timestamp
#include <iostream> // for debugging
#include <iostream>
uint64_t get_timestamp_ms() {
using namespace std::chrono;
return duration_cast<milliseconds>(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;
}

View File

@ -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

View File

@ -1,6 +1,3 @@
#ifndef PROCESS_LAUNCHER_H
#define PROCESS_LAUNCHER_H
#ifdef _WIN32
#include <windows.h>
#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