Add header.
[amitay/samba.git] / source4 / lib / appweb / ejs-2.0 / ejs / system / ejsSystemLog.c
1 /*
2  *      @file   ejsSystemLog.c
3  *      @brief  System.Log class for the EJS Object Model
4  */
5 /********************************** Copyright *********************************/
6 /*
7  *      Copyright (c) Mbedthis Software LLC, 2005-2006. All Rights Reserved.
8  */
9 /********************************** Includes **********************************/
10
11 #include        "ejs.h"
12
13 /*********************************** Usage ************************************/
14 /*
15  *      System.Log.setLog(path);
16  *      System.Log.enable;
17  */
18 /******************************************************************************/
19
20 static void logHandler(MPR_LOC_DEC(ctx, loc), int flags, int level, 
21         const char *msg)
22 {
23         MprApp  *app;
24         char    *buf;
25         int             len;
26
27         app = mprGetApp(ctx);
28         if (app->logFile == 0) {
29                 return;
30         }
31
32         if (flags & MPR_LOG_SRC) {
33                 len = mprAllocSprintf(MPR_LOC_PASS(ctx, loc), &buf, 0, 
34                         "Log %d: %s\n", level, msg);
35
36         } else if (flags & MPR_ERROR_SRC) {
37                 len = mprAllocSprintf(MPR_LOC_PASS(ctx, loc), &buf, 0, 
38                         "Error: %s\n", msg);
39
40         } else if (flags & MPR_FATAL_SRC) {
41                 len = mprAllocSprintf(MPR_LOC_PASS(ctx, loc), &buf, 0, 
42                         "Fatal: %s\n", msg);
43                 
44         } else if (flags & MPR_ASSERT_SRC) {
45 #if BLD_FEATURE_ALLOC_LEAK_TRACK
46                 len = mprAllocSprintf(MPR_LOC_PASS(ctx, loc), &buf, 0, 
47                         "Assertion %s, failed at %s\n",
48                         msg, loc);
49 #else
50                 len = mprAllocSprintf(MPR_LOC_PASS(ctx, loc), &buf, 0, 
51                         "Assertion %s, failed\n", msg);
52 #endif
53
54         } else if (flags & MPR_RAW) {
55                 /* OPT */
56                 len = mprAllocSprintf(MPR_LOC_PASS(ctx, loc), &buf, 0, 
57                         "%s", msg);
58
59         } else {
60                 return;
61         }
62
63         mprPuts(app->logFile, buf, len);
64
65         mprFree(buf);
66 }
67
68 /******************************************************************************/
69 /************************************ Methods *********************************/
70 /******************************************************************************/
71 /*
72  *      function int setLog(string path)
73  */
74
75 static int setLog(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
76 {
77         const char      *path;
78         MprFile         *file;
79         MprApp          *app;
80
81         if (argc != 1 || !ejsVarIsString(argv[0])) {
82                 ejsArgError(ejs, "Usage: setLog(path)");
83                 return -1;
84         }
85
86         app = mprGetApp(ejs);
87
88         /*
89          *      Ignore errors if we can't create the log file.
90          *      Use the app context so this will live longer than the interpreter
91          *      MOB -- this leaks files.
92          */
93         path = argv[0]->string;
94         file = mprOpen(app, path, O_CREAT | O_TRUNC | O_WRONLY, 0664);
95         if (file) {
96                 app->logFile = file;
97                 mprSetLogHandler(ejs, logHandler);
98         }
99         mprLog(ejs, 0, "Test log");
100
101         return 0;
102 }
103
104 /******************************************************************************/
105 #if UNUSED
106
107 static int enableSetAccessor(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
108 {
109         if (argc != 1) {
110                 ejsArgError(ejs, "Usage: set(value)");
111                 return -1;
112         }
113         ejsSetProperty(ejs, thisObj, "_enabled", argv[0]);
114         return 0;
115 }
116
117 /******************************************************************************/
118
119 static int enableGetAccessor(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
120 {
121         ejsSetReturnValue(ejs, ejsGetPropertyAsVar(ejs, thisObj, "_enabled"));
122         return 0;
123 }
124
125 #endif
126 /******************************************************************************/
127 /******************************** Initialization ******************************/
128 /******************************************************************************/
129
130 int ejsDefineLogClass(Ejs *ejs)
131 {
132         EjsVar                  *logClass;
133
134         logClass =  ejsDefineClass(ejs, "System.Log", "Object", 0);
135         if (logClass == 0) {
136                 return MPR_ERR_CANT_INITIALIZE;
137         }
138
139         ejsDefineCMethod(ejs, logClass, "setLog", setLog, EJS_NO_LOCAL);
140
141 #if UNUSED
142         EjsProperty             *pp;
143         ejsDefineCAccessors(ejs, logClass, "enable", enableSetAccessor, 
144                 enableGetAccessor, EJS_NO_LOCAL);
145
146         pp = ejsSetPropertyToBoolean(ejs, logClass, "_enabled", 0);
147         ejsMakePropertyEnumerable(pp, 0);
148 #endif
149
150         return ejsObjHasErrors(logClass) ? MPR_ERR_CANT_INITIALIZE : 0;
151 }
152
153 /******************************************************************************/
154
155 /*
156  * Local variables:
157  * tab-width: 4
158  * c-basic-offset: 4
159  * End:
160  * vim:tw=78
161  * vim600: sw=4 ts=4 fdm=marker
162  * vim<600: sw=4 ts=4
163  */