warn.cpp

来自「伯克利做的SFTP安全文件传输协议」· C++ 代码 · 共 95 行

CPP
95
字号
// warn.cc// code for warn.h// copyright SafeTP Development Group, Inc., 2000  Terms of use are as specified in license.txt#include "warn.h"       // this module#include "typ.h"        // bool#include "breaker.h"    // breaker#include <stdio.h>      // fopen, stderr, etc.#include <time.h>       // time, ctime// thread safety: assumption is that all of these are set at global init time// globalsWarningHandler warningHandler = defaultWarningHandler;#ifdef NDEBUG  WarnLevel logWarnLevel = (WarnLevel)(WARN_ALL - WARN_DEBUG);  WarnLevel displayWarnLevel = WARN_NONE;#else  WarnLevel logWarnLevel = WARN_ALL;  WarnLevel displayWarnLevel = WARN_ALL;#endifvoid warning(WarnLevel level, char const *message){  warningHandler(level, message);}void defaultWarningHandler(WarnLevel level, char const *message){  if (level & WARN_DEBUG) {    // hit a breakpoint if the debugger is attached    breaker();  }  if (level & logWarnLevel) {    defaultWarningLogger(level, message);  }  if (level & logWarnLevel) {    defaultWarningPrinter(level, message);  }}void defaultWarningLogger(WarnLevel /*level*/, char const *message){  // thread safety: we could call fopen with the same filename more  // than once.. I have no idea how that behaves, but it's hard to  // imagine any behavior leading to catastrophic results..  static FILE *logfile = NULL;  static bool failedToOpen = false;  if (!logfile && !failedToOpen) {    logfile = fopen("warning.log", "a");    if (!logfile) {      // don't keep trying      failedToOpen = true;    }    else {      // start with a timestamp      time_t t;      time(&t);      int len = fprintf(logfile, "\nLog started at %s", ctime(&t));        // note: astonishingly (bad), the string returned by ctime() has        //       a newline at the end!      while (len--) {        fprintf(logfile, "-");      }      fprintf(logfile, "\n");    }  }  if (logfile) {    // append the message to the logfile    fprintf(logfile, "warning: %s\n", message);    fflush(logfile);  }}void defaultWarningPrinter(WarnLevel /*level*/, char const *message){  fprintf(stderr, "warning: %s\n", message);  fflush(stderr);}// no test code because it is my judgment that bugs in this// module will be easily evident, and it is a very simple// module, so it isn't worth it to separately test

⌨️ 快捷键说明

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