⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 logfile.cpp

📁 一个非常好的人工智能开发工具开源软件
💻 CPP
字号:
/*** *** See the file "L2_RTI_EO1/disclaimers-and-notices-L2.txt" for  *** information on usage and redistribution of this file,  *** and for a DISCLAIMER OF ALL WARRANTIES. ***/XF#include "LogFile.hpp"#include <string.h>            // for strchr(), strncpy()#include "ExceptionAction.hpp"#include "Logging.hpp"#include "ioLib.h"LogFile::LogFile(const char* pathname,                 unsigned bytesPerVersion,                 unsigned numberOfVersions) :  d_pathname(pathname),  d_bytesPerVersion(bytesPerVersion),  d_numberOfVersions(numberOfVersions),  d_currentVersion(0),  d_byteCount(0),  d_file(0) {  // assert(pathname != 0 && strlen(pathname != 0);  // assert(bytesPerVersion > 0);  // assert(numberOfVersions > 0);}LogFile::~LogFile() {  // The log file streams should be closed explicitly, but just in case...  closeFile();}// Insert the version number before the last 0void LogFile::makeVersionPathname(char versionPathname[]) {  // Find the last period in the pathname  const char* lastPeriod = strchr(d_pathname, '.');  if (lastPeriod == 0) {    // There is no period in the pathname; append the version number    sprintf(versionPathname, "%s.v%d", d_pathname, d_currentVersion);  } else {    // There is a period in the pathname; insert the version number before it    const int prefixLength = strlen(d_pathname) - strlen(lastPeriod);    // The part before the last period    strncpy(versionPathname, d_pathname, prefixLength);    sprintf(versionPathname + prefixLength,            "%i.%s",            d_currentVersion, // the version number            lastPeriod + 1);  // the extension  }}void LogFile::assureOpen() {  if (d_file == 0) {    char versionPathname[256];    makeVersionPathname(versionPathname);    if ((d_file = creat(versionPathname, O_WRONLY)) == 0) {      ExceptionAction::logFileOpenFailure(versionPathname);    }  }}void LogFile::writeToDisk(const LOG_MSG& logMsg) {  int strSize;  char str [MAX_MSG_LENGTH+1];  strSize = sprintf(str, "%s\n", logMsg.msg);  #if DEBUG_LEVEL==3    puts(str);  // output to the serial port too.  #endif  // error check  if ((d_byteCount += strSize) >= d_bytesPerVersion) {    // Change version    d_currentVersion = ++d_currentVersion % d_numberOfVersions;    // Close the old file; it will be re-opened when needed    closeFile();    // Reset the byte count    d_byteCount = strSize;  }  assureOpen();  if (d_file == 0) {    ExceptionAction::noLogFile(logMsg.msg, logMsg.msgType);  } else {    write(d_file, str, strSize);    ioctl (d_file, FIOFLUSH, 0);  // force flushing out to file.  }}void LogFile::closeFile() {  if (d_file != 0) {    ioctl (d_file, FIOFLUSH, 0);  // force flushing out to file.    close(d_file);    d_file = 0;  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -