/[dtapublic]/to_be_filed/webprojs/php_libraries/php_library/pamc_a/logging/log.inc
ViewVC logotype

Contents of /to_be_filed/webprojs/php_libraries/php_library/pamc_a/logging/log.inc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 35 - (show annotations) (download)
Sat Oct 8 23:35:33 2016 UTC (8 years, 1 month ago) by dashley
File size: 7458 byte(s)
Initial commit.
1 <?php
2 //********************************************************************************
3 //Copyright (C)2006 David T. Ashley
4 //********************************************************************************
5 //This program or source file is free software; you can redistribute it and/or
6 //modify it under the terms of the GNU General Public License as published by
7 //the Free Software Foundation; either version 2 of the License, or (at your
8 //option) any later version.
9 //
10 //This program or source file is distributed in the hope that it will
11 //be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 //GNU General Public License for more details.
14 //
15 //You may have received a copy of the GNU General Public License
16 //along with this program; if not, write to the Free Software
17 //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 //********************************************************************************
19 //Dave Ashley, 01/06
20 //
21 //This source file is a wrapper for functions that emit information into the
22 //log files.
23 //
24 //Replaces unruly characters with the underscore.
25 //
26 function LOG_replace_unruly_chars($arg)
27 {
28 $allowed_chars = "abcdefghijklmnopqrstuvwxyz" .
29 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" .
30 "0123456789" .
31 " .,_:;-+*/" .
32 "()[]";
33
34 $len = strlen($arg);
35 $rv = "";
36
37 for ($i = 0; $i < $len; $i++)
38 {
39 $c = substr($arg, $i, 1);
40
41 if (strpos($allowed_chars, $c) === FALSE)
42 {
43 //The character involved isn't in the allowed set. Replace it with an underscore.
44 $rv .= "_";
45 }
46 else
47 {
48 //The character involved is allowed. Put it directly in the result.
49 $rv .= $c;
50 }
51 }
52
53 return($rv);
54 }
55 //
56 //
57 //Writes environmental information to the log file that may be helpful in error or
58 //security debugging.
59 //
60 function LOG_log_environment($prefix)
61 {
62 //Copy the server arguments. Don't want to accidentally change something.
63 $tgt = $_SERVER;
64
65 //Reset to first value.
66 reset($tgt);
67
68 //Loop through, emitting args.
69 while (list($key, $val) = each($tgt))
70 {
71 syslog(
72 LOG_NOTICE,
73 $prefix
74 . "_SERVER[\""
75 . $key
76 . "\"]: "
77 . $val
78 );
79 }
80 //-----------------------------------------------------------------------
81 //Copy the environment arguments. Don't want to accidentally change something.
82 $tgt = $_ENV;
83
84 //Reset to first value.
85 reset($tgt);
86
87 //Loop through, emitting args.
88 while (list($key, $val) = each($tgt))
89 {
90 syslog(
91 LOG_NOTICE,
92 $prefix
93 . "_ENV[\""
94 . $key
95 . "\"]: "
96 . $val
97 );
98 }
99 //-----------------------------------------------------------------------
100 //Copy the request arguments. Don't want to accidentally change something.
101 $tgt = $_REQUEST;
102
103 //Reset to first value.
104 reset($tgt);
105
106 //Loop through, emitting args.
107 while (list($key, $val) = each($tgt))
108 {
109 syslog(
110 LOG_NOTICE,
111 $prefix
112 . "_REQUEST[\""
113 . $key
114 . "\"]: "
115 . $val
116 );
117 }
118 }
119 //
120 //
121 //Writes the debug backtrace information to the log file.
122 //
123 function LOG_log_debug_backtrace($prefix)
124 {
125 //Grab backtrace.
126 $bt = debug_backtrace();
127
128 //Iterate through each element.
129 reset($bt);
130 $i=0;
131 while (list($key, $val) = each($bt))
132 {
133 //Spit the function name.
134 if (isset($val["function"]))
135 syslog(LOG_NOTICE, $prefix . "FUNCTION[" . sprintf("%02d", $i) . "]: " . $val["function"]);
136 else
137 syslog(LOG_NOTICE, $prefix . "No function name specified in traceback.");
138
139 //Spit the file name.
140 if (isset($val["file"]))
141 syslog(LOG_NOTICE, $prefix . "FILE [" . sprintf("%02d", $i) . "]: " . $val["file"]);
142 else
143 syslog(LOG_NOTICE, $prefix . "No file name specified in traceback.");
144
145 //Spit the line number.
146 if (isset($val["line"]))
147 syslog(LOG_NOTICE, $prefix . "LINE [" . sprintf("%02d", $i) . "]: " . $val["line"]);
148 else
149 syslog(LOG_NOTICE, $prefix . "No line number specified in traceback.");
150
151 $i++;
152 }
153 }
154 //
155 //
156 //Writes a horizontal line to the log file.
157 //
158 function LOG_log_hline($prefix)
159 {
160 $val = $prefix;
161
162 for ($i=0; $i<80; $i++)
163 {
164 $val .= "-";
165 }
166
167 error_log($val);
168 }
169 //
170 //Emits "hard" error messages. An "error" for these purposes is a very unexpected event,
171 //such as not being able to connect to a database, conditions that should not
172 //occur, etc. This includes a backtrace.
173 //
174 function LOG_error_hard($msgstring)
175 {
176 $prefix = "PAMC_ERROR_HARD: ";
177
178 //Sanitize the input string to allow only certain characters.
179 $msgstring = LOG_replace_unruly_chars($msgstring);
180
181 //Open with a horizontal line.
182 LOG_log_hline($prefix);
183
184 //Insert the message.
185 syslog(LOG_NOTICE, $prefix . $msgstring);
186
187 //Do the backtrace.
188 LOG_log_debug_backtrace($prefix);
189
190 //Log the environment.
191 LOG_log_environment($prefix);
192
193 //Close with a horizontal line.
194 LOG_log_hline($prefix);
195 }
196 //
197 //Emits "soft" error messages. An "error" for these purposes is a very unexpected event,
198 //such as not being able to connect to a database, conditions that should not
199 //occur, etc. This does not include a backtrace.
200 //
201 function LOG_error_soft($msgstring)
202 {
203 $prefix = "PAMC_ERROR_SOFT: ";
204
205 //Sanitize the input string to allow only certain characters.
206 $msgstring = LOG_replace_unruly_chars($msgstring);
207
208 //Insert the message.
209 syslog(LOG_NOTICE, $prefix . $msgstring);
210 }
211 //
212 //Emits security messages. An "security" message is something security-related.
213 //
214 function LOG_security($msgstring)
215 {
216 $prefix = "PAMC_SECURITY: ";
217
218 //Sanitize the input string to allow only certain characters.
219 $msgstring = LOG_replace_unruly_chars($msgstring);
220
221 //Open with a horizontal line.
222 LOG_log_hline($prefix);
223
224 //Insert the message.
225 syslog(LOG_NOTICE, $prefix . $msgstring);
226
227 //Log the environment.
228 LOG_log_environment($prefix);
229
230 //Close with a horizontal line.
231 LOG_log_hline($prefix);
232 }
233 //
234 //Emits alert messages. An "alert" message is something significant that one
235 //might want to look into.
236 //
237 function LOG_alert($msgstring)
238 {
239 $prefix = "PAMC_ALERT: ";
240
241 //Sanitize the input string to allow only certain characters.
242 $msgstring = LOG_replace_unruly_chars($msgstring);
243
244 //Insert the message.
245 syslog(LOG_NOTICE, $prefix . $msgstring);
246 }
247 //
248 //Emits informational messages. An "informational" message is just something ordinary
249 //that occurs.
250 //
251 function LOG_info($msgstring)
252 {
253 $prefix = "PAMC_INFO: ";
254
255 //Sanitize the input string to allow only certain characters.
256 $msgstring = LOG_replace_unruly_chars($msgstring);
257
258 //Insert the message.
259 syslog(LOG_NOTICE, $prefix . $msgstring);
260 }
261 ?>

dashley@gmail.com
ViewVC Help
Powered by ViewVC 1.1.25