]> rtime.felk.cvut.cz Git - arc.git/blob - common/ramlog.c
Merged in from default
[arc.git] / common / ramlog.c
1 /* -------------------------------- Arctic Core ------------------------------\r
2  * Arctic Core - the open source AUTOSAR platform http://arccore.com\r
3  *\r
4  * Copyright (C) 2009  ArcCore AB <contact@arccore.com>\r
5  *\r
6  * This source code is free software; you can redistribute it and/or modify it\r
7  * under the terms of the GNU General Public License version 2 as published by the\r
8  * Free Software Foundation; See <http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt>.\r
9  *\r
10  * This program is distributed in the hope that it will be useful, but\r
11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
12  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License\r
13  * for more details.\r
14  * -------------------------------- Arctic Core ------------------------------*/\r
15 \r
16 /**\r
17  * A very simple ramlog.\r
18  *\r
19  * Features:\r
20  * - Prints to a ram space in section ".ramlog"\r
21  * - The size is configurable using CFG_RAMLOG_SIZE (default is 1000)\r
22  * - The ramlog section should not be cleared by the linkfile if one wants\r
23  *   to have a ramlog that survives reset.\r
24  * - Session support\r
25  *\r
26  * Assumes some c-lib support:\r
27  * - The clib support should be able to open a file called "ramlog".\r
28  * - Printing to that file will print to the ramlog.\r
29  *\r
30  * HEADER\r
31  *\r
32  *   The ramlog uses a very simple header.\r
33  *\r
34  *  Byte   0    1    2    3\r
35  *       +----+----+----+----+\r
36  *       | 01 |CNT | X  |    |\r
37  *       +----+----+----+----+\r
38  *\r
39  *   Byte 0 is 01\r
40  *   Byte 1 is a counter that incremented for each time the ramlog is started.\r
41  *   Byte 2 is not defined yet.\r
42  *   Byte 3 is 01\r
43  *\r
44  *   01 is used since it's control sign and not a printable char.\r
45  *\r
46  * STRATEGY FOR SESSION\r
47  *   Cases:\r
48  *   1. The ramlog is empty, ie the header is not there at first position\r
49  *   2. The header is in first poistion\r
50  *      - look for the next?\r
51  *      - Use saved inforation?\r
52  *\r
53  *\r
54  */\r
55 \r
56 #include <stdio.h>\r
57 #include <stdarg.h>\r
58 \r
59 \r
60 \r
61 #ifndef CFG_RAMLOG_SIZE\r
62 #define CFG_RAMLOG_SIZE  2000\r
63 #endif\r
64 \r
65 #define RAMLOG_MAGIC      1\r
66 \r
67 \r
68 static unsigned char ramlog[CFG_RAMLOG_SIZE] __attribute__ ((section (".ramlog")));\r
69 #if defined(CFG_RAMLOG_SESSION)\r
70 static unsigned ramlog_curr __attribute__ ((section (".ramlog")));\r
71 static unsigned ramlog_session __attribute__ ((section (".ramlog")));\r
72 #else\r
73 static unsigned ramlog_curr = 0;\r
74 #endif\r
75 \r
76 #define RAMLOG_FILENO  (FILE *)3\r
77 \r
78 \r
79 /**\r
80  * Print a char to the ramlog\r
81  * @param c\r
82  */\r
83 void ramlog_chr( char c ) {\r
84   ramlog[ramlog_curr++] = c;\r
85   if( ramlog_curr >= CFG_RAMLOG_SIZE ) {\r
86           ramlog_curr = 0;\r
87   }\r
88 }\r
89 \r
90 /**\r
91  * Print a string to the ramlog\r
92  * @param str\r
93  */\r
94 void ramlog_puts( char *str ) {\r
95 \r
96   while(*str!=0) {\r
97         ramlog_chr(*str++);\r
98   }\r
99   ramlog_chr('\n');\r
100 }\r
101 \r
102 /**\r
103  * Formatted print for the ramlog.\r
104  *\r
105  * @param format The format string.\r
106  */\r
107 // extern int standard_simple_sprintf(int fd, char *out, const char *format, ...);\r
108 void ramlog_printf( const char *format, ... ) {\r
109 \r
110         // Fast and ugly ramlog support.\r
111         volatile int rv;\r
112         va_list args;\r
113         va_start(args,format);\r
114 \r
115         rv = vfprintf(RAMLOG_FILENO, format, args);\r
116         va_end(args);\r
117 }\r
118 \r
119 \r
120 /**\r
121  * Initialize the ramlog. Must be called before any other ramlog functions.\r
122  */\r
123 void ramlog_init()\r
124 {\r
125 \r
126 #if defined(CFG_RAMLOG_SESSION)\r
127         char buf[32];\r
128         /* Check for existing session */\r
129         if( (ramlog[0] != RAMLOG_MAGIC) || (ramlog[3] != RAMLOG_MAGIC) ) {\r
130                 ramlog_curr = 0;\r
131                 ramlog_session = 0;\r
132         } else {\r
133                 /*  Search the ramlog */\r
134         }\r
135     ramlog_session++;\r
136     simple_sprintf(buf, "Session (%d)\n", ramlog_session);\r
137     ramlog_puts(buf);\r
138 \r
139 #else\r
140         ramlog_curr = 0;\r
141 #endif\r
142 }\r