Initial import of work-in-progress of Poseidon port.
[cake.git] / rom / usb / poseidon / PsdErrorlog.c
blobb148078c9083bf8454bc7d8bacf58a02db9949ee
1 /*
2 ** PsdErrorlog by Chris Hodges <chrisly@platon42.de>
3 */
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <exec/exec.h>
9 #include <dos/datetime.h>
10 #include <libraries/poseidon.h>
11 #include <proto/poseidon.h>
12 #include <proto/exec.h>
13 #include <proto/dos.h>
15 #define ARGS_NOFLUSH 0
16 #define ARGS_NOTS 1
17 #define ARGS_SIZEOF 2
19 static const char *template = "NOFLUSH/S,NOTIMESTAMPS=NOTS/S";
20 static const char *version = "$VER: PsdErrorlog 4.0 (03.06.09) by Chris Hodges <chrisly@platon42.de>";
21 static IPTR ArgsArray[ARGS_SIZEOF];
22 static struct RDArgs *ArgsHook = NULL;
24 void fail(char *str)
26 if(ArgsHook)
28 FreeArgs(ArgsHook);
29 ArgsHook = NULL;
31 if(str)
33 PutStr(str);
34 exit(20);
36 exit(0);
39 int main(int argc, char *argv[])
41 struct Library *ps;
42 struct List *errmsgs;
43 struct Node *pem;
44 ULONG level;
45 IPTR origin;
46 IPTR errstr;
47 STRPTR errmsg = NULL;
48 struct DateStamp *ds;
49 struct DateTime dt;
50 struct DateStamp currdate;
51 UBYTE strdate[LEN_DATSTRING];
52 UBYTE strtime[LEN_DATSTRING];
54 if(!(ArgsHook = ReadArgs(template, ArgsArray, NULL)))
55 fail("Wrong arguments!\n");
57 if((ps = OpenLibrary("poseidon.library", 3)))
59 DateStamp(&currdate);
60 if(ArgsArray[ARGS_NOFLUSH])
62 psdLockReadPBase();
63 } else {
64 psdLockWritePBase();
66 psdGetAttrs(PGA_STACK, NULL, PA_ErrorMsgList, &errmsgs, TAG_END);
67 pem = errmsgs->lh_Head;
68 while(pem->ln_Succ)
70 ds = NULL;
71 psdGetAttrs(PGA_ERRORMSG, pem,
72 EMA_Level, &level,
73 EMA_Origin, &origin,
74 EMA_Msg, &errstr,
75 EMA_DateStamp, &ds,
76 TAG_END);
77 if(ds && (!ArgsArray[ARGS_NOTS]))
79 dt.dat_Stamp.ds_Days = ds->ds_Days;
80 dt.dat_Stamp.ds_Minute = ds->ds_Minute;
81 dt.dat_Stamp.ds_Tick = ds->ds_Tick;
82 dt.dat_Format = FORMAT_DEF;
83 dt.dat_Flags = 0;
84 dt.dat_StrDay = NULL;
85 dt.dat_StrDate = strdate;
86 dt.dat_StrTime = strtime;
87 DateToStr(&dt);
88 if(currdate.ds_Days == ds->ds_Days)
90 Printf("%s| %2ld-%s: %s\n", strtime, level, origin, errstr);
91 } else {
92 Printf("%s %s| %2ld-%s: %s\n", strdate, strtime, level, origin, errstr);
94 } else {
95 Printf("%2ld-%s: %s\n", level, origin, errstr);
97 pem = pem->ln_Succ;
99 if(!ArgsArray[ARGS_NOFLUSH])
101 Forbid();
102 while(errmsgs->lh_Head->ln_Succ)
104 psdRemErrorMsg(errmsgs->lh_Head);
106 Permit();
108 psdUnlockPBase();
109 CloseLibrary(ps);
110 } else {
111 errmsg = "Unable to open poseidon.library\n";
113 fail(errmsg);
114 return(0); // never gets here, just to shut the compiler up