save
This commit is contained in:
parent
d5c0c20a9e
commit
733b36837f
@ -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)
|
@ -3,7 +3,7 @@
|
|||||||
int LogWriter::write_log(const std::string& log_line) {
|
int LogWriter::write_log(const std::string& log_line) {
|
||||||
std::string new_line = "\n";
|
std::string new_line = "\n";
|
||||||
int fd = open(
|
int fd = open(
|
||||||
"log.txt",
|
"main.log",
|
||||||
O_WRONLY|O_APPEND|O_CREAT
|
O_WRONLY|O_APPEND|O_CREAT
|
||||||
);
|
);
|
||||||
if (fd == -1) {
|
if (fd == -1) {
|
||||||
@ -17,7 +17,67 @@ int LogWriter::write_log(const std::string& log_line) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int LogWriter::log_start_line(int pid){
|
std::string LogWriter::get_current_time() {
|
||||||
write_log("hello");
|
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;
|
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);
|
||||||
}
|
}
|
@ -3,13 +3,21 @@
|
|||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
|
||||||
|
#include <ctime>
|
||||||
|
|
||||||
|
|
||||||
class LogWriter {
|
class LogWriter {
|
||||||
public:
|
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:
|
private:
|
||||||
static int write_log(const std::string& log_line);
|
static int write_log(const std::string& log_line);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
@ -1,6 +1,13 @@
|
|||||||
|
#include "process_launcher.h"
|
||||||
#include "log_writer.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[]) {
|
int main(int argc, char* argv[]) {
|
||||||
if (argc >= 2) {
|
if (argc >= 2) {
|
||||||
@ -15,8 +22,32 @@ int main(int argc, char* argv[]) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << "launch main part\n";
|
LogWriter::log_start_line();
|
||||||
int res = LogWriter::log_start_line(1);
|
|
||||||
std::cout << res;
|
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;
|
return 0;
|
||||||
}
|
}
|
@ -32,6 +32,16 @@ int ProcessLauncher::waitForProcessWindows(HANDLE processHandle) {
|
|||||||
return -1;
|
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
|
#else
|
||||||
|
|
||||||
// Для UNIX-подобных систем используем fork и exec
|
// Для UNIX-подобных систем используем fork и exec
|
||||||
@ -73,4 +83,13 @@ int ProcessLauncher::waitForProcessUnix(pid_t pid) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int ProcessLauncher::isProcessActive(int pid) {
|
||||||
|
return isProcessActiveUnix(pid);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ProcessLauncher::isProcessActiveUnix(pid_t pid) {
|
||||||
|
// TODO
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
@ -1,6 +1,3 @@
|
|||||||
#ifndef PROCESS_LAUNCHER_H
|
|
||||||
#define PROCESS_LAUNCHER_H
|
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
#else
|
#else
|
||||||
@ -19,16 +16,19 @@ public:
|
|||||||
// Ожидание завершения процесса и получение его кода возврата
|
// Ожидание завершения процесса и получение его кода возврата
|
||||||
static int waitForProcess(int pid);
|
static int waitForProcess(int pid);
|
||||||
|
|
||||||
|
// проверка запущен ли процесс
|
||||||
|
static int isProcessActive(int pid);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
// Для Windows
|
// Для Windows
|
||||||
static int launchWindows(const std::string& command);
|
static int launchWindows(const std::string& command);
|
||||||
static int waitForProcessWindows(HANDLE processHandle);
|
static int waitForProcessWindows(HANDLE processHandle);
|
||||||
|
static int isProcessActiveWindows(HANDLE processHandle);
|
||||||
#else
|
#else
|
||||||
// Для UNIX-подобных систем
|
// Для UNIX-подобных систем
|
||||||
static int launchUnix(const std::string& command);
|
static int launchUnix(const std::string& command);
|
||||||
static int waitForProcessUnix(pid_t pid);
|
static int waitForProcessUnix(pid_t pid);
|
||||||
|
static int isProcessActiveUnix(pid_t pid);
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // PROCESS_LAUNCHER_H
|
|
Loading…
x
Reference in New Issue
Block a user