hello-world-cpp/task_3/log_writer.cpp

74 lines
1.6 KiB
C++
Raw Permalink Normal View History

2025-01-10 11:17:15 +10:00
#include "log_writer.h"
2025-01-13 19:59:39 +10:00
2025-01-10 11:17:15 +10:00
int LogWriter::write_log(const std::string& log_line) {
std::string new_line = "\n";
int fd = open(
2025-01-10 14:44:36 +10:00
"main.log",
2025-01-10 16:59:44 +00:00
O_WRONLY|O_APPEND|O_CREAT,
0777
2025-01-10 11:17:15 +10:00
);
if (fd == -1) {
return -1; // Ошибка при открытии файла
}
write(fd, log_line.c_str(), log_line.length());
write(fd, new_line.c_str(), new_line.length());
2025-01-11 02:17:31 +10:00
close(fd);
2025-01-10 11:17:15 +10:00
return 0;
}
2025-01-10 16:59:44 +00:00
void LogWriter::log_start_line(){
2025-01-10 14:44:36 +10:00
std::string log_line = (
"start programm with pid " +
std::to_string(getpid()) +
" at " +
2025-01-13 19:59:39 +10:00
MyTime::get_current_time()
2025-01-10 14:44:36 +10:00
);
write_log(log_line);
}
2025-01-10 16:59:44 +00:00
void LogWriter::log_counter(long long int counter){
2025-01-10 14:44:36 +10:00
std::string log_line = (
"counter at " +
2025-01-13 19:59:39 +10:00
MyTime::get_current_time() +
2025-01-10 14:44:36 +10:00
" in process with pid " +
std::to_string(getpid()) +
" is " +
std::to_string(counter)
);
write_log(log_line);
}
2025-01-10 16:59:44 +00:00
void LogWriter::log_copy_start(std::string copy_name) {
2025-01-10 14:44:36 +10:00
std::string log_line = (
copy_name +
" is started at " +
2025-01-13 19:59:39 +10:00
MyTime::get_current_time() +
2025-01-10 14:44:36 +10:00
" with pid " +
std::to_string(getpid())
);
write_log(log_line);
}
2025-01-10 16:59:44 +00:00
void LogWriter::log_copy_finish(std::string copy_name) {
2025-01-10 14:44:36 +10:00
std::string log_line = (
copy_name +
" is finished at " +
2025-01-13 19:59:39 +10:00
MyTime::get_current_time()
2025-01-10 14:44:36 +10:00
);
write_log(log_line);
}
2025-01-10 16:59:44 +00:00
void LogWriter::log_copy_error(std::string copy_name) {
2025-01-10 14:44:36 +10:00
std::string log_line = (
copy_name +
" is not finished"
);
write_log(log_line);
2025-01-10 11:17:15 +10:00
}