update task 3, go to counter in file
This commit is contained in:
parent
8b938e3cda
commit
2595b0b7b7
7
.gitignore
vendored
7
.gitignore
vendored
@ -9,4 +9,9 @@ _trash/
|
|||||||
|
|
||||||
# auto-generating trash from cmake
|
# auto-generating trash from cmake
|
||||||
build/
|
build/
|
||||||
CMakeFiles/
|
CMakeFiles/
|
||||||
|
Makefile
|
||||||
|
CMakeCache.txt
|
||||||
|
*.cmake
|
||||||
|
*.a
|
||||||
|
|
||||||
|
@ -5,7 +5,8 @@ project(hello-world-cpp-logger)
|
|||||||
|
|
||||||
add_library(log_writer STATIC log_writer.cpp)
|
add_library(log_writer STATIC log_writer.cpp)
|
||||||
add_library(process_launcher STATIC process_launcher.cpp)
|
add_library(process_launcher STATIC process_launcher.cpp)
|
||||||
|
add_library(counter_manager STATIC counter_manager.cpp)
|
||||||
|
|
||||||
add_executable(main main.cpp)
|
add_executable(main main.cpp)
|
||||||
|
|
||||||
target_link_libraries(main log_writer process_launcher)
|
target_link_libraries(main log_writer process_launcher counter_manager)
|
1
task_3/counter.txt
Normal file
1
task_3/counter.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
233
|
40
task_3/counter_manager.cpp
Normal file
40
task_3/counter_manager.cpp
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
#include "counter_manager.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
long long int CounterManager::get_counter() {
|
||||||
|
char data_from_file[128];
|
||||||
|
int num_bytes = 128;
|
||||||
|
|
||||||
|
int fd = open(
|
||||||
|
"counter.txt",
|
||||||
|
_O_RDONLY
|
||||||
|
);
|
||||||
|
if (fd == -1) {
|
||||||
|
return 0; // Ошибка при открытии файла
|
||||||
|
}
|
||||||
|
|
||||||
|
read(fd, data_from_file, num_bytes);
|
||||||
|
|
||||||
|
close(fd);
|
||||||
|
|
||||||
|
return atoi(data_from_file);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int CounterManager::set_counter(long long int new_counter) {
|
||||||
|
std::string new_counter_string = std::to_string(new_counter);
|
||||||
|
int fd = open(
|
||||||
|
"counter.txt",
|
||||||
|
O_WRONLY|O_TRUNC|O_APPEND|O_CREAT,
|
||||||
|
0777
|
||||||
|
);
|
||||||
|
if (fd == -1) {
|
||||||
|
return -1; // Ошибка при открытии файла
|
||||||
|
}
|
||||||
|
|
||||||
|
write(fd, new_counter_string.c_str(), new_counter_string.length());
|
||||||
|
close(fd);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
12
task_3/counter_manager.h
Normal file
12
task_3/counter_manager.h
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
#include <string>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
|
|
||||||
|
class CounterManager {
|
||||||
|
public:
|
||||||
|
static long long int get_counter();
|
||||||
|
static int set_counter(long long int new_counter);
|
||||||
|
|
||||||
|
};
|
@ -13,6 +13,8 @@ int LogWriter::write_log(const std::string& log_line) {
|
|||||||
write(fd, log_line.c_str(), log_line.length());
|
write(fd, log_line.c_str(), log_line.length());
|
||||||
write(fd, new_line.c_str(), new_line.length());
|
write(fd, new_line.c_str(), new_line.length());
|
||||||
|
|
||||||
|
close(fd);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -36,7 +38,7 @@ int LogWriter::log_start_line(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int LogWriter::log_counter(int counter){
|
int LogWriter::log_counter(long long int counter){
|
||||||
std::string current_time = "00:00:00";
|
std::string current_time = "00:00:00";
|
||||||
std::string log_line = (
|
std::string log_line = (
|
||||||
"counter at " +
|
"counter at " +
|
||||||
@ -51,7 +53,7 @@ int LogWriter::log_counter(int counter){
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int LogWriter::log_copy_start(int counter, std::string copy_name) {
|
int LogWriter::log_copy_start(std::string copy_name) {
|
||||||
std::string current_time = "00:00:00";
|
std::string current_time = "00:00:00";
|
||||||
std::string log_line = (
|
std::string log_line = (
|
||||||
copy_name +
|
copy_name +
|
||||||
|
@ -9,8 +9,8 @@
|
|||||||
class LogWriter {
|
class LogWriter {
|
||||||
public:
|
public:
|
||||||
static int log_start_line();
|
static int log_start_line();
|
||||||
static int log_counter(int counter);
|
static int log_counter(long long int counter);
|
||||||
static int log_copy_start(int counter, std::string copy_name);
|
static int log_copy_start(std::string copy_name);
|
||||||
static int log_copy_finish(std::string copy_name);
|
static int log_copy_finish(std::string copy_name);
|
||||||
static int log_copy_error(std::string copy_name);
|
static int log_copy_error(std::string copy_name);
|
||||||
|
|
||||||
|
@ -1,8 +1,35 @@
|
|||||||
#include "process_launcher.h"
|
#include "process_launcher.h"
|
||||||
#include "log_writer.h"
|
#include "log_writer.h"
|
||||||
|
#include "counter_manager.h"
|
||||||
#include <chrono> // for ms in timestamp
|
#include <chrono> // for ms in timestamp
|
||||||
#include <iostream> // for debugging
|
#include <iostream> // for debugging
|
||||||
|
|
||||||
|
struct two_pid {
|
||||||
|
int pid_1, pid_2;
|
||||||
|
};
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
// Для Windows
|
||||||
|
struct two_pid launch_copies() {
|
||||||
|
std::string command1 = "cmd /C main.exe 1";
|
||||||
|
std::string command2 = "cmd /C main.exe 2";
|
||||||
|
struct two_pid result;
|
||||||
|
result.pid_1 = ProcessLauncher::launch(command1);
|
||||||
|
result.pid_2 = ProcessLauncher::launch(command2);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
// Для UNIX-подобных систем
|
||||||
|
struct two_pid launch_copies() {
|
||||||
|
std::string command1 = "main 1";
|
||||||
|
std::string command2 = "main 2";
|
||||||
|
struct two_pid result;
|
||||||
|
result.pid_1 = ProcessLauncher::launch(command1);
|
||||||
|
result.pid_2 = ProcessLauncher::launch(command2);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
uint64_t get_timestamp_ms() {
|
uint64_t get_timestamp_ms() {
|
||||||
using namespace std::chrono;
|
using namespace std::chrono;
|
||||||
@ -12,19 +39,26 @@ uint64_t get_timestamp_ms() {
|
|||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
if (argc >= 2) {
|
if (argc >= 2) {
|
||||||
if (argv[1][0] == '1') {
|
if (argv[1][0] == '1') {
|
||||||
std::cout << "start copy 1";
|
LogWriter::log_copy_start("Copy 1");
|
||||||
|
CounterManager::set_counter(CounterManager::get_counter() + 10);
|
||||||
|
LogWriter::log_copy_finish("Copy 1");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (argv[1][0] == '2') {
|
if (argv[1][0] == '2') {
|
||||||
std::cout << "start copy 2";
|
LogWriter::log_copy_start("Copy 2");
|
||||||
|
CounterManager::set_counter(CounterManager::get_counter() * 2);
|
||||||
|
// sleep 2s
|
||||||
|
CounterManager::set_counter(CounterManager::get_counter() / 2);
|
||||||
|
LogWriter::log_copy_finish("Copy 2");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
LogWriter::log_start_line();
|
LogWriter::log_start_line();
|
||||||
|
|
||||||
long long int counter = 0;
|
struct two_pid pid_of_copy_processes;
|
||||||
|
|
||||||
uint64_t last_counter_increment = get_timestamp_ms();
|
uint64_t last_counter_increment = get_timestamp_ms();
|
||||||
uint64_t last_counter_logging = get_timestamp_ms();
|
uint64_t last_counter_logging = get_timestamp_ms();
|
||||||
uint64_t last_copies_launching = get_timestamp_ms();
|
uint64_t last_copies_launching = get_timestamp_ms();
|
||||||
@ -35,18 +69,16 @@ int main(int argc, char* argv[]) {
|
|||||||
|
|
||||||
if ((current_time - last_counter_increment) >= 300) {
|
if ((current_time - last_counter_increment) >= 300) {
|
||||||
last_counter_increment = current_time;
|
last_counter_increment = current_time;
|
||||||
counter++;
|
CounterManager::set_counter(CounterManager::get_counter() + 1);
|
||||||
}
|
}
|
||||||
if ((current_time - last_counter_logging) >= 1000) {
|
if ((current_time - last_counter_logging) >= 1000) {
|
||||||
last_counter_logging = current_time;
|
last_counter_logging = current_time;
|
||||||
LogWriter::log_counter(counter);
|
LogWriter::log_counter(CounterManager::get_counter());
|
||||||
}
|
}
|
||||||
if ((current_time - last_copies_launching) >= 3000) {
|
if ((current_time - last_copies_launching) >= 3000) {
|
||||||
last_copies_launching = current_time;
|
last_copies_launching = current_time;
|
||||||
|
pid_of_copy_processes = launch_copies();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user