1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15:
16: 17: 18: 19:
20:
21: #include <basic.h>
22: #include <tk/tkernel.h>
23: #include <tk/util.h>
24: #include <tm/tmonitor.h>
25: #include <sys/debug.h>
26: #include "syslog.h"
27:
28: #if USE_SYSLOG_CONSIO
29: #include <sys/consio.h>
30: #endif
31:
32: EXPORT W logtask_alive = 0;
33:
34: #if USE_SYSLOG_CONSIO
35: LOCAL ID log_mbfid;
36: #endif
37:
38:
39: #define OBJNAME_SYSLOG "SLog"
40:
41: 42: 43:
44: LOCAL void sys_write( const char *s, int len )
45: {
46: while ( len-- > 0 ) {
47: tm_putchar(*s++);
48: }
49: }
50:
51: 52: 53:
54: LOCAL ER svc_syslog_wait( void )
55: {
56: #if USE_SYSLOG_CONSIO
57: T_RMBF rmbf;
58: W i;
59: ER ercd;
60:
61: if ( logtask_alive ) {
62: for ( i = 0; i < 50; i++ ) {
63: tk_dly_tsk(100);
64: ercd = tk_ref_mbf(log_mbfid, &rmbf);
65: if ( ercd != E_OK || rmbf.wtsk > 0 ) {
66: break;
67: }
68: }
69: }
70: #endif
71:
72: return E_OK;
73: }
74:
75: 76: 77:
78: EXPORT ER __syslog_send( const char *string, int len )
79: {
80: if ( string == NULL ) {
81: return svc_syslog_wait();
82: }
83:
84: if ( len <= 0 ) {
85: return E_OK;
86: }
87:
88: #if USE_SYSLOG_CONSIO
89: if ( logtask_alive ) {
90: ER ercd;
91:
92:
93: if ( len > MBF_LOG_MAXMSZ ) {
94: len = MBF_LOG_MAXMSZ;
95: }
96: ercd = tk_snd_mbf(log_mbfid, (void*)string, len, TMO_POL);
97: if ( ercd == E_OK ) {
98: return E_OK;
99: }
100: logtask_alive = 0;
101: }
102: #endif
103:
104: 105:
106: sys_write(string, len);
107: sys_write("\n", 1);
108:
109: return E_OK;
110: }
111:
112: #if USE_SYSLOG_CONSIO
113: 114: 115:
116: LOCAL void log_task( INT logtask_port )
117: {
118: static B logtask_buf[MBF_LOG_MAXMSZ+1];
119: INT msgsz;
120: ER ercd;
121:
122: logtask_alive = 1;
123:
124: for ( ;; ) {
125: ercd = tk_rcv_mbf(log_mbfid, logtask_buf, TMO_FEVR);
126: if ( ercd < E_OK ) {
127: break;
128: }
129: msgsz = ercd;
130: logtask_buf[msgsz++] = '\n';
131:
132: ercd = console_out(logtask_port, logtask_buf, (UW)msgsz);
133: if ( ercd < E_OK ) {
134: sys_write(logtask_buf, msgsz);
135: }
136: }
137:
138: logtask_alive = 0;
139: tk_exd_tsk();
140: }
141: #endif
142:
143: 144: 145:
146: EXPORT ER initialize_syslog( void )
147: {
148: #if USE_SYSLOG_CONSIO
149: T_CMBF cmbf;
150: T_CTSK ctsk;
151: ID tskid;
152: ER ercd;
153:
154:
155: SetOBJNAME(cmbf.exinf, OBJNAME_SYSLOG);
156: cmbf.mbfatr = TA_TFIFO | TA_NODISWAI;
157: cmbf.bufsz = MBF_LOG_BUFSZ;
158: cmbf.maxmsz = MBF_LOG_MAXMSZ;
159: ercd = tk_cre_mbf(&cmbf);
160: if ( ercd < E_OK ) {
161: goto err_ret1;
162: }
163: log_mbfid = ercd;
164:
165: 166:
167: tk_chg_pri(TSK_SELF, 10);
168:
169:
170: SetOBJNAME(ctsk.exinf, OBJNAME_SYSLOG);
171: ctsk.tskatr = TA_HLNG | TA_RNG0;
172: ctsk.task = (FP)log_task;
173: ctsk.itskpri = 6;
174: ctsk.stksz = 512;
175: ercd = tk_cre_tsk(&ctsk);
176: if ( ercd < E_OK ) {
177: goto err_ret1;
178: }
179: tskid = ercd;
180: ercd = tk_sta_tsk(tskid, CONSOLE_PORT);
181: if ( ercd < E_OK ) {
182: goto err_ret2;
183: }
184:
185:
186: tk_chg_pri(TSK_SELF, TPRI_INI);
187:
188: return E_OK;
189:
190: err_ret2:
191: tk_del_tsk(tskid);
192: err_ret1:
193: DEBUG_PRINT(("initialize_syslog ercd = %d\n", ercd));
194: return ercd;
195: #else
196: return E_OK;
197: #endif
198: }
199:
200: 201: 202:
203: EXPORT ER finish_syslog( void )
204: {
205: #if USE_SYSLOG_CONSIO
206: ER ercd;
207:
208: 209:
210: ercd = tk_del_mbf(log_mbfid);
211:
212: #ifdef DEBUG
213: if ( ercd < E_OK ) {
214: DEBUG_PRINT(("finish_syslog ercd = %d\n", ercd));
215: }
216: #endif
217: return ercd;
218: #else
219: return E_OK;
220: #endif
221: }