00001 // Filename: notifyCategory.cxx 00002 // Created by: drose (29Feb00) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved 00008 // 00009 // All use of this software is subject to the terms of the Panda 3d 00010 // Software license. You should have received a copy of this license 00011 // along with this source code; you will also find a current copy of 00012 // the license at http://www.panda3d.org/license.txt . 00013 // 00014 // To contact the maintainers of this program write to 00015 // panda3d@yahoogroups.com . 00016 // 00017 //////////////////////////////////////////////////////////////////// 00018 00019 #include "notifyCategory.h" 00020 #include "notify.h" 00021 #include "config_notify.h" 00022 00023 #include <time.h> // for strftime(). 00024 #include <assert.h> 00025 00026 time_t NotifyCategory::_server_delta = 0; 00027 00028 //////////////////////////////////////////////////////////////////// 00029 // Function: NotifyCategory::Constructor 00030 // Access: Private 00031 // Description: 00032 //////////////////////////////////////////////////////////////////// 00033 NotifyCategory:: 00034 NotifyCategory(const string &fullname, const string &basename, 00035 NotifyCategory *parent) : 00036 _fullname(fullname), 00037 _basename(basename), 00038 _parent(parent) 00039 { 00040 if (_parent != (NotifyCategory *)NULL) { 00041 _parent->_children.push_back(this); 00042 } 00043 00044 _severity = NS_unspecified; 00045 00046 // See if there's a config option to set the severity level for this 00047 // Category. 00048 00049 string config_name; 00050 00051 if (_fullname.empty()) { 00052 config_name = "notify-level"; 00053 00054 } else if (!_basename.empty()) { 00055 config_name = "notify-level-" + _basename; 00056 } 00057 00058 if (!config_name.empty()) { 00059 string severity_name; 00060 if (!config_notify.AmInitializing()) 00061 severity_name = config_notify.GetString(config_name, ""); 00062 if (!severity_name.empty()) { 00063 // The user specified a particular severity for this category at 00064 // config time. Use it. 00065 _severity = Notify::string_severity(severity_name); 00066 00067 if (_severity == NS_unspecified) { 00068 nout << "Invalid severity name for " << config_name << ": " 00069 << severity_name << "\n"; 00070 } 00071 } 00072 } 00073 00074 if (_severity == NS_unspecified) { 00075 // If we didn't get an explicit severity level, inherit our 00076 // parent's. 00077 if (_parent != (NotifyCategory *)NULL) { 00078 _severity = _parent->_severity; 00079 00080 } else { 00081 // Unless, of course, we're the root. 00082 _severity = NS_info; 00083 } 00084 } 00085 00086 // Only the unnamed top category is allowed not to have a parent. 00087 nassertv(_parent != (NotifyCategory *)NULL || _fullname.empty()); 00088 } 00089 00090 //////////////////////////////////////////////////////////////////// 00091 // Function: NotifyCategory::out 00092 // Access: Published 00093 // Description: Begins a new message to this Category at the 00094 // indicated severity level. If the indicated severity 00095 // level is enabled, this writes a prefixing string to 00096 // the Notify::out() stream and returns that. If the 00097 // severity level is disabled, this returns 00098 // Notify::null(). 00099 //////////////////////////////////////////////////////////////////// 00100 ostream &NotifyCategory:: 00101 out(NotifySeverity severity, bool prefix) const { 00102 if (is_on(severity)) { 00103 if (prefix) { 00104 if (get_notify_timestamp()) { 00105 // Format a timestamp to include as a prefix as well. 00106 time_t now = time(NULL) + _server_delta; 00107 struct tm *ptm = localtime(&now); 00108 00109 char buffer[128]; 00110 strftime(buffer, 128, ":%m-%d-%Y %H:%M:%S ", ptm); 00111 nout << buffer; 00112 } 00113 00114 if (severity == NS_info) { 00115 return nout << *this << ": "; 00116 } else { 00117 return nout << *this << "(" << severity << "): "; 00118 } 00119 } else { 00120 return nout; 00121 } 00122 } else { 00123 return Notify::null(); 00124 } 00125 } 00126 00127 //////////////////////////////////////////////////////////////////// 00128 // Function: NotifyCategory::get_num_children 00129 // Access: Published 00130 // Description: Returns the number of child Categories of this 00131 // particular Category. 00132 //////////////////////////////////////////////////////////////////// 00133 int NotifyCategory:: 00134 get_num_children() const { 00135 return _children.size(); 00136 } 00137 00138 //////////////////////////////////////////////////////////////////// 00139 // Function: NotifyCategory::get_child 00140 // Access: Published 00141 // Description: Returns the nth child Category of this particular 00142 // Category. 00143 //////////////////////////////////////////////////////////////////// 00144 NotifyCategory *NotifyCategory:: 00145 get_child(int i) const { 00146 assert(i >= 0 && i < (int)_children.size()); 00147 return _children[i]; 00148 } 00149 00150 //////////////////////////////////////////////////////////////////// 00151 // Function: NotifyCategory::set_server_delta 00152 // Access: Published, Static 00153 // Description: Sets a global delta (in seconds) between the local 00154 // time and the server's time, for the purpose of 00155 // synchronizing the time stamps in the log messages of 00156 // the client with that of a known server. 00157 //////////////////////////////////////////////////////////////////// 00158 void NotifyCategory:: 00159 set_server_delta(time_t delta) { 00160 _server_delta = delta; 00161 }