bool ApplicationLog::createLogFile() { this -> currentDate = QDate::currentDate(); QString logFilePath = this -> getLogFilePath(this -> currentDate); writeToStdout(QString("Path to the log file: \"%1\".").arg(logFilePath)); this -> logFile -> setFileName(logFilePath); if (!this -> logFile -> open(QFile::ReadWrite | QFile::Append)) { writeToStdout(QString("An error occurred while opening the log file: (%1) %2.").arg(this -> logFile -> error()).arg(this -> logFile -> errorString())); return false; } return true; }
bool ApplicationLog::initialize(QString logFilesDirectoryPath) { writeToStdout(QString("Path to the directory for the log files: \"%1\".").arg(logFilesDirectoryPath)); if (!QDir(logFilesDirectoryPath).exists()) { writeToStdout(QString("This directory does not exist.")); return false; } this -> logFilesDirectoryPath = logFilesDirectoryPath; this -> logFile = new QFile(); if (!this -> createLogFile()) { return false; } this -> logMutex = new QMutex(); return true; }
static inline void writeFmtArgsToStdout(const char* fmt, va_list args) { if(fmt == NULL) { writeToStdout("(null)"); } else { vprintf(fmt, args); } }
void i_kslog_c(const char* level, const char* file, unsigned int line, const char* function, const char* fmt, ...) { if(*fmt != 0) { char buffer[KSLogger_CBufferSize]; snprintf(buffer, sizeof(buffer), "%s: %s (%u): %s: ", level, lastPathEntry(file), line, function); writeToStdout(buffer); va_list args; va_start(args,fmt); vsnprintf(buffer, sizeof(buffer), fmt, args); va_end(args); writeToStdout(buffer); write(STDOUT_FILENO, "\n", 1); } }
void i_kslog_c_basic(const char* fmt, ...) { if(*fmt != 0) { char buffer[KSLogger_CBufferSize]; va_list args; va_start(args,fmt); vsnprintf(buffer, sizeof(buffer), fmt, args); va_end(args); writeToStdout(buffer); write(STDOUT_FILENO, "\n", 1); } }
void ApplicationLog::write(QString message) { QString datetimeString = QDateTime::currentDateTime().toString(LOG_MESSAGE_DATETIME_PATTERN); QString formattedMessage = QString("%1 %2").arg(datetimeString, message); this -> logMutex -> lock(); // если новая дата if (this -> currentDate != QDate::currentDate()) { writeToStdout(QString("The current log file will be closed and a new file opened for the new date.")); this -> logFile -> close(); if (!this -> createLogFile()) { return; } } QTextStream stdoutStream(stdout); QTextStream logStream(logFile); stdoutStream << formattedMessage << endl; logStream << formattedMessage << endl; stdoutStream.flush(); logStream.flush(); logMutex -> unlock(); }
static void connectClient(const char *dev) { fd_set master; fd_set read_fds; FD_ZERO(&master); FD_ZERO(&read_fds); signal(SIGHUP, exit_handler); signal(SIGINT, exit_handler); signal(SIGQUIT, exit_handler); signal(SIGPIPE, exit_handler); signal(SIGTSTP, exit_handler); signal(SIGTERM, exit_handler); int stdi; struct ttyRaw* tty_in; stdi = STDIN_FILENO; if (!isatty(stdi)) { return; } tty_in = setTty(stdi, 0); int stdo; struct ttyRaw* tty_out; stdo = STDOUT_FILENO; if (!isatty(stdo)) { closeTty(tty_in); return; } tty_out = setTty(stdo, 0); int clientfd; clientfd = createClientSocket(dev); if (clientfd < 0) { closeTty(tty_out); closeTty(tty_in); return; } FD_SET(tty_in->fd,&master); FD_SET(clientfd, &master); int fdmax = 0; fdmax = (clientfd > tty_in->fd) ? clientfd : tty_in->fd; for(;;) { if (sigexit) { break; } read_fds = master; if (select(fdmax + 1, &read_fds, NULL, NULL, NULL) == -1) { perror("mTerm_client: select error"); break; } if (FD_ISSET(stdi, &read_fds)) { if (!readFromStdin(clientfd, tty_in->fd)) { break; } } if (FD_ISSET(clientfd, &read_fds)) { if (!writeToStdout(clientfd,tty_out->fd)) { break; } } } closeTty(tty_out); closeTty(tty_in); close(clientfd); }