PPLogger.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /**
  2. * PANDA 3D SOFTWARE
  3. * Copyright (c) Carnegie Mellon University. All rights reserved.
  4. *
  5. * All use of this software is subject to the terms of the revised BSD
  6. * license. You should have received a copy of this license along
  7. * with this source code in a file named "LICENSE."
  8. *
  9. * @file PPLogger.cpp
  10. * @author atrestman
  11. * @date 2009-09-14
  12. */
  13. #include "stdafx.h"
  14. #include "PPLogger.h"
  15. #include "mkdir_complete.h"
  16. #include "wstring_encode.h"
  17. #include <windows.h>
  18. std::ofstream PPLogger::m_logfile;
  19. bool PPLogger::m_isOpen = false;
  20. PPLogger::PPLogger( )
  21. {
  22. }
  23. PPLogger::~PPLogger( )
  24. {
  25. }
  26. void PPLogger::Open( const std::string &rootDir )
  27. {
  28. if (!m_isOpen) {
  29. // Note that this logfile name may not be specified at runtime. It must
  30. // be compiled in if it is specified at all.
  31. std::string log_directory;
  32. // Allow the developer to compile in the log directory.
  33. #ifdef P3D_PLUGIN_LOG_DIRECTORY
  34. if (log_directory.empty()) {
  35. log_directory = P3D_PLUGIN_LOG_DIRECTORY;
  36. }
  37. #endif
  38. // Failing that, we write logfiles to Panda3Dlog.
  39. if (log_directory.empty()) {
  40. log_directory = rootDir + "/log";
  41. }
  42. mkdir_complete(log_directory, std::cerr);
  43. // Ensure that the log directory ends with a slash.
  44. if (!log_directory.empty() && log_directory[log_directory.size() - 1] != '/') {
  45. #ifdef _WIN32
  46. if (log_directory[log_directory.size() - 1] != '\\')
  47. #endif
  48. log_directory += "/";
  49. }
  50. // Construct the logfile pathname.
  51. std::string log_basename;
  52. #ifdef P3D_PLUGIN_LOG_BASENAME1
  53. if (log_basename.empty()) {
  54. log_basename = P3D_PLUGIN_LOG_BASENAME1;
  55. }
  56. #endif
  57. if (log_basename.empty()) {
  58. log_basename = "p3dplugin";
  59. }
  60. if (!log_basename.empty()) {
  61. std::string log_pathname = log_directory;
  62. log_pathname += log_basename;
  63. log_pathname += ".log";
  64. m_logfile.close();
  65. m_logfile.clear();
  66. wstring log_pathname_w;
  67. string_to_wstring(log_pathname_w, log_pathname);
  68. m_logfile.open(log_pathname_w.c_str(), std::ios::out | std::ios::trunc);
  69. m_logfile.setf(std::ios::unitbuf);
  70. }
  71. // If we didn't have a logfile name compiled in, we throw away log output
  72. // by the simple expedient of never actually opening the ofstream.
  73. m_isOpen = true;
  74. }
  75. }