The example in this section demonstrates how to implement brief-log, a custom SAF for logging only three items of information about a request: the IP address, the method, and the URI (for example, 198.93.95.99 GET /jocelyn/dogs/homesneeded.html).
To load the shared object containing your functions, add the following line in the Init section of the magnus.conf file:
Init fn=load-modules shlib=yourlibrary funcs=brief-init,brief-log
To call brief-init to open the log file, add the following line to the Init section in magnus.conf. (This line must come after the one that loads the library containing brief-init.)
Init fn=brief-init file=/tmp/brief.log
To execute your custom SAF during the AddLog stage for some object, add the following line to that object in the obj.conf file:
AddLog fn=brief-log
The source code is in addlog.c file in the nsapi/examples/ or plugins/nsapi/examples subdirectory within the server root directory.
#include "nsapi.h" #include "base/daemon.h" /* daemon_atrestart */ #include "base/file.h" /* system_fopenWA, system_fclose */ #include "base/util.h" /* sprintf */ /* File descriptor to be shared between the processes */ static SYS_FILE logfd = SYS_ERROR_FD; #ifdef __cplusplus extern "C" #endif NSAPI_PUBLIC void brief_terminate(void *parameter) { system_fclose(logfd); logfd = SYS_ERROR_FD; } #ifdef __cplusplus extern "C" #endif NSAPI_PUBLIC int brief_init(pblock *pb, Session *sn, Request *rq) { /* Parameter */ char *fn = pblock_findval("file", pb); if(!fn) { pblock_nvinsert("error", "brief-init: please supply a file name", pb); return REQ_ABORTED; } logfd = system_fopenWA(fn); if(logfd == SYS_ERROR_FD) { pblock_nvinsert("error", "brief-init: please supply a file name", pb); return REQ_ABORTED; } /* Close log file when server is restarted */ daemon_atrestart(brief_terminate, NULL); return REQ_PROCEED; } #ifdef __cplusplus extern "C" #endif NSAPI_PUBLIC int brief_log(pblock *pb, Session *sn, Request *rq) { /* No parameters */ /* Server data */ char *method = pblock_findval("method", rq->reqpb); char *uri = pblock_findval("uri", rq->reqpb); char *ip = pblock_findval("ip", sn->client); /* Temp vars */ char *logmsg; int len; logmsg = (char *) MALLOC(strlen(ip) + 1 + strlen(method) + 1 + strlen(uri) + 1 + 1); len = util_sprintf(logmsg, "%s %s %s\n", ip, method, uri); /* The atomic version uses locking to prevent interference */ system_fwrite_atomic(logfd, logmsg, len); FREE(logmsg); return REQ_PROCEED; }