ClassLogFile.cpp 992 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #include "ClassLogFile.h"
  2. #include "time_sntp.h"
  3. ClassLogFile LogFile("/sdcard/log.txt");
  4. void ClassLogFile::WriteToDedicatedFile(std::string _fn, std::string info, bool _time)
  5. {
  6. FILE* pFile;
  7. std::string zwtime;
  8. if (!doLogFile){
  9. return;
  10. }
  11. pFile = fopen(_fn.c_str(), "a+");
  12. if (_time)
  13. {
  14. time_t rawtime;
  15. struct tm* timeinfo;
  16. char buffer[80];
  17. time(&rawtime);
  18. timeinfo = localtime(&rawtime);
  19. strftime(buffer, 80, "%Y-%m-%d_%H-%M-%S", timeinfo);
  20. zwtime = std::string(buffer);
  21. info = zwtime + ": " + info;
  22. }
  23. fputs(info.c_str(), pFile);
  24. fputs("\n", pFile);
  25. fclose(pFile);
  26. }
  27. void ClassLogFile::SwitchOnOff(bool _doLogFile){
  28. doLogFile = _doLogFile;
  29. };
  30. void ClassLogFile::WriteToFile(std::string info, bool _time)
  31. {
  32. WriteToDedicatedFile(logfile, info, _time);
  33. }
  34. ClassLogFile::ClassLogFile(std::string _logfile)
  35. {
  36. logfile = _logfile;
  37. doLogFile = true;
  38. }