File: Logger.k

package info (click to toggle)
kaya 0.4.2-4
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 4,448 kB
  • ctags: 1,694
  • sloc: cpp: 9,536; haskell: 7,461; sh: 3,013; yacc: 910; makefile: 816; perl: 90
file content (114 lines) | stat: -rw-r--r-- 4,264 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/** -*-C-*-ish
    Kaya standard library
    Copyright (C) 2004, 2005 Edwin Brady

    This file is distributed under the terms of the GNU Lesser General
    Public Licence. See COPYING for licence.
*/
"<summary>Message Logging</summary>
<prose>This module allows messages to be logged to a file or an in-memory String. On platforms other than Windows, the <moduleref>Syslog</moduleref> module may also be of use.</prose>"
module Logger;

import Prelude;
import public IO;
import Time;
import Strings;

"<summary>Logging method.</summary>
<prose><code>LogFile</code> is for sending logs directly to a file; it is less efficient because it opens and closes the file each time logging is done; use <code>LogHandle</code> for heavy logging. <code>LogString</code> is for logging to a string in memory. The given string is modified when it is logged to.</prose>"
public data Logger 
    = LogFile(String fname)
    | LogHandle(File handle)
    | LogString(String log);
    // CIM: shouldn't this be 'abstract' rather than 'public'?

"<argument name='fname'>The filename</argument>
<summary>Create a log file</summary>
<prose>Create a log file and return the logging method.
The file will not actually be created, if it doesn't already exist, until it is logged to.</prose>
<related><dataref>Logger</dataref></related>
<related><functionref>makeLogHandle</functionref></related>
<related><functionref>makeLogString</functionref></related>"
public Logger makeLogFile(String fname)
{
    return LogFile(fname);
}

"<argument name='handle'>The file handle</argument>
<summary>Create a logging method from a file handle</summary>
<prose>Creates a logging method from a file handle. If the file handle is not opened for either writing or appending then this will silently fail.</prose>
<related><dataref>Logger</dataref></related>
<related><functionref>makeLogFile</functionref></related>
<related><functionref>makeLogString</functionref></related>"
public Logger makeLogHandle(File handle)
{
    return LogHandle(handle);
}

"<summary>Create an in-memory string logging method</summary>
<prose>Create a string logging method, initialised to the empty string.</prose>
<related><dataref>Logger</dataref></related>
<related><functionref>makeLogFile</functionref></related>
<related><functionref>makeLogHandle</functionref></related>"
public Logger makeLogString()
{
    return LogString("");
}

"<argument name='l'>The logging method</argument>
<summary>Finish logging.</summary>
<prose>Finish logging. This has no effect for files or strings, but closes the file handle.</prose>"
public Void endLog(Logger l)
{
    case l of {
	LogFile(fn) -> ;
	| LogString(s) -> ;
	| LogHandle(f) -> close(f);
    }
}

String leadzero(Int x) | x<10 = "0"+x
                       | default = String(x);

"<argument name='l'>A <dataref>Logger</dataref></argument>
<argument name='msg'>The message to log</argument>
<argument name='timestamp'>If true (the default) a timestamp will be prepended to the message before it is logged.</argument>
<summary>Add to a log.</summary>
<prose>Add a message to a log.</prose>
<related><functionref>logAll</functionref></related>"
public Void log(var Logger l, String msg, Bool timestamp = true)
{
    logmsg = msg +"\n";

    if (timestamp) {
	t = localTime();
	stamp = "[" + substr(string(t.mon),0,3) + " " + t.mday + " " +
	    leadzero(t.hour) + ":" + leadzero(t.minute) + ":" +
	    leadzero(t.second) + "]";
	logmsg = stamp + " " + logmsg;        
    }

    case l of {
	LogFile(fn) -> 
	    f = open(fn,[Append]);
	    put(f,logmsg);
	    close(f);
	| LogHandle(f) ->
	    put(f,logmsg);
	| LogString(s) ->
	    l.log += logmsg;
    }
}

"<argument name='ls'>A list of <dataref>Logger</dataref>s</argument>
<argument name='msg'>The message to log</argument>
<argument name='timestamp'>If true (the default) a timestamp will be prepended to the message before it is logged.</argument>
<summary>Add to multiple logs at once.</summary>
<prose>Add a message to multiple logs at once (useful for logging to a log file and standard error simultaneously, for example).</prose>
<related><functionref>logAll</functionref></related>"
public Void logAll(var [Logger] ls, String msg, Bool timestamp = true)
{
    for l in ls {
	log(l,msg, timestamp);
    }
}