3 * @brief System.Log class for the EJS Object Model
5 /********************************** Copyright *********************************/
7 * Copyright (c) Mbedthis Software LLC, 2005-2006. All Rights Reserved.
9 /********************************** Includes **********************************/
13 /*********************************** Usage ************************************/
15 * System.Log.setLog(path);
18 /******************************************************************************/
20 static void logHandler(MPR_LOC_DEC(ctx, loc), int flags, int level,
28 if (app->logFile == 0) {
32 if (flags & MPR_LOG_SRC) {
33 len = mprAllocSprintf(MPR_LOC_PASS(ctx, loc), &buf, 0,
34 "Log %d: %s\n", level, msg);
36 } else if (flags & MPR_ERROR_SRC) {
37 len = mprAllocSprintf(MPR_LOC_PASS(ctx, loc), &buf, 0,
40 } else if (flags & MPR_FATAL_SRC) {
41 len = mprAllocSprintf(MPR_LOC_PASS(ctx, loc), &buf, 0,
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",
50 len = mprAllocSprintf(MPR_LOC_PASS(ctx, loc), &buf, 0,
51 "Assertion %s, failed\n", msg);
54 } else if (flags & MPR_RAW) {
56 len = mprAllocSprintf(MPR_LOC_PASS(ctx, loc), &buf, 0,
63 mprPuts(app->logFile, buf, len);
68 /******************************************************************************/
69 /************************************ Methods *********************************/
70 /******************************************************************************/
72 * function int setLog(string path)
75 static int setLog(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
81 if (argc != 1 || !ejsVarIsString(argv[0])) {
82 ejsArgError(ejs, "Usage: setLog(path)");
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.
93 path = argv[0]->string;
94 file = mprOpen(app, path, O_CREAT | O_TRUNC | O_WRONLY, 0664);
97 mprSetLogHandler(ejs, logHandler);
99 mprLog(ejs, 0, "Test log");
104 /******************************************************************************/
107 static int enableSetAccessor(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
110 ejsArgError(ejs, "Usage: set(value)");
113 ejsSetProperty(ejs, thisObj, "_enabled", argv[0]);
117 /******************************************************************************/
119 static int enableGetAccessor(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
121 ejsSetReturnValue(ejs, ejsGetPropertyAsVar(ejs, thisObj, "_enabled"));
126 /******************************************************************************/
127 /******************************** Initialization ******************************/
128 /******************************************************************************/
130 int ejsDefineLogClass(Ejs *ejs)
134 logClass = ejsDefineClass(ejs, "System.Log", "Object", 0);
136 return MPR_ERR_CANT_INITIALIZE;
139 ejsDefineCMethod(ejs, logClass, "setLog", setLog, EJS_NO_LOCAL);
143 ejsDefineCAccessors(ejs, logClass, "enable", enableSetAccessor,
144 enableGetAccessor, EJS_NO_LOCAL);
146 pp = ejsSetPropertyToBoolean(ejs, logClass, "_enabled", 0);
147 ejsMakePropertyEnumerable(pp, 0);
150 return ejsObjHasErrors(logClass) ? MPR_ERR_CANT_INITIALIZE : 0;
153 /******************************************************************************/
161 * vim600: sw=4 ts=4 fdm=marker