A Tuesday 07 October 2008 17:44:54, Pawel Veselov wrote:
> Here is an updated version.
>
I checked it in, after fixing a few issues I noticed --- there was
an off-by-one error, and the WCETRACING macro wasn't guarding accesses
to the `level' argument with `()'. Can't say I'm thrilled by the
hardcoded string lengths constants, but I decided to leave it in.
I've also tweaked your change log entry for you.
+
+ newlib/
+ * libc/sys/wince/trace.c (__trace_closed): New.
+ (get_level_name): New.
+ (WCETRACE): Rename to...
+ (__WCETrace): ... this.
+ (WCETRACESET): Do not change the trace level if the tracing
+ facilities have been closed.
+ (WCETRACECLOSE): Print out a message that the tracing has closed.
+ Reset tracing level to 0 after closing.
+ (__WCETrace): Print out the tracing level along with the message.
+ * libc/sys/wince/sys/wcetrace.h (WCETRACE): Define.
+ (WCETRACE): Rename to ...
+ (__WCETrace): ... this.
+ (WCETRACING): Define.
+ * libc/sys/wince/io.c (_write_r): When tracing out stdio/stderr
+ buffer, send out as many bytes as are in the buffer, instead of
+ using zero termination.
Don't repeat file names. Don't explain why you did things on a
changelog entry. That belongs in the code. E.g., get_level_name was described
in
the changelog entry, but not in the code, so I just comented it there
instead (also moved it higher in the file to avoid needing the forward
declaration).
Attached is what I checked in. Thanks!
--
Pedro Alves
Index: newlib/libc/sys/wince/trace.c
===================================================================
--- newlib/libc/sys/wince/trace.c (revisão 1199)
+++ newlib/libc/sys/wince/trace.c (revisão 1200)
@@ -15,15 +15,17 @@
static HANDLE __wcetracehnd = NULL;
static int __wcetrace = 0;
static int __wcetrace_debugger = 0; /* Used to be WCE_ALL */
+static int __trace_closed = 0;
void
WCETRACESET(int trace)
{
- __wcetrace = trace;
+ if (!__trace_closed)
+ __wcetrace = trace;
}
int
-WCETRACEGET()
+WCETRACEGET(void)
{
return(__wcetrace);
}
@@ -35,7 +37,7 @@ WCETRACE_DEBUGGER_SET(int trace)
}
int
-WCETRACE_DEBUGGER_GET()
+WCETRACE_DEBUGGER_GET(void)
{
return(__wcetrace_debugger);
}
@@ -46,23 +48,75 @@ typedef struct
int flag;
} trace_entry;
+/* This define specifies how long a buffer is needed for displaying
+ all the levels. It should be the sum of all names of level
+ lengths, plus the amount of level lengths (for the separation
+ commas) plus 6 (currently) digits for displaying the numeric value
+ of the level. And then add a terminating 0, and a safety padding.
+ The WCE_ALL is not accounted for, as it consumes all other levels,
+ and never shows in the output. */
+
+#define MAX_LEVEL_BUF (41 + 9 + 6 + 10)
+
static const trace_entry trace_entries[] =
{
{ "all", WCE_ALL },
- { "io", WCE_IO },
- { "network", WCE_NETWORK },
- { "signals", WCE_SIGNALS },
- { "fifos", WCE_FIFOS },
- { "time", WCE_TIME },
- { "synch", WCE_SYNCH },
- { "malloc", WCE_MALLOC },
- { "vm", WCE_VM },
- { "app", WCE_APP },
-
+ { "io", WCE_IO }, /* 2 = 2 */
+ { "network", WCE_NETWORK }, /* 7 = 9 */
+ { "signals", WCE_SIGNALS }, /* 7 = 16 */
+ { "fifos", WCE_FIFOS }, /* 5 = 21 */
+ { "time", WCE_TIME }, /* 4 = 25 */
+ { "synch", WCE_SYNCH }, /* 5 = 30 */
+ { "malloc", WCE_MALLOC }, /* 6 = 36 */
+ { "vm", WCE_VM }, /* 2 = 38 */
+ { "app", WCE_APP }, /* 3 = 41 */
+
{ NULL, 0 }
};
+/* Convert LEVEL into a comma separated string, and store it in TO.
+ TO must be at least MAX_LEVEL_BUF long. */
+
+static void
+get_level_name(int level, char *to)
+{
+ int virgin = 1;
+
+ const trace_entry *ent = trace_entries + 1;
+ for (; ent->str; ent++)
+ {
+ if (level & ent->flag)
+ {
+ int len;
+
+ level &= ~ent->flag;
+ if (!virgin)
+ *(to++) = ',';
+ else
+ virgin = 0;
+
+ len = strlen(ent->str);
+ memcpy(to, ent->str, len);
+ to += len;
+ }
+ }
+
+ if (level)
+ {
+ int printed;
+
+ /* There were some unresolved flags. */
+ if (!virgin)
+ *(to++) = ',';
+
+ printed = sprintf(to, "%#04.4x", level);
+ to += printed;
+ }
+
+ *to = 0;
+}
+
void NKDbgPrintfA(const char *fmt, ...);
static void set_from_env(const char* env, int* what)
@@ -111,33 +165,44 @@ static void set_from_env(const char* env
}
void
-WCETRACEGETENV()
+WCETRACEGETENV(void)
{
set_from_env("WCETRACE", &__wcetrace);
set_from_env("WCETRACE_DEBUGGER", &__wcetrace_debugger);
}
void
-WCETRACECLOSE()
+WCETRACECLOSE(void)
{
if (__wcetracehnd != NULL && __wcetracehnd != INVALID_HANDLE_VALUE)
{
+ __wcetrace = WCE_IO;
+ __WCETrace(WCE_IO, "Trace file is being closed");
FlushFileBuffers(__wcetracehnd);
XCECloseHandle(__wcetracehnd);
__wcetracehnd = NULL;
+ __trace_closed = 1;
+ __wcetrace = 0;
}
}
void
-WCETRACE(int level, const char *fmt, ...)
+__WCETrace(int level, const char *fmt, ...)
{
+ int len;
+ char level_name[MAX_LEVEL_BUF];
+ char buf[1024];
+
+ va_list ap;
+
if (!(__wcetrace_debugger & level) && !(__wcetrace & level))
return;
- char buf[1024];
- int len = sprintf(buf, "%08X:%08X: ", GetTickCount(), GetCurrentThreadId());
+ get_level_name(level, level_name);
+ len = sprintf(buf, "%08X:%08X: [%s] ",
+ GetTickCount(), GetCurrentThreadId(),
+ level_name);
- va_list ap;
va_start(ap, fmt);
vsprintf(buf + len, fmt, ap);
strcat(buf, "\n");
@@ -148,7 +213,7 @@ WCETRACE(int level, const char *fmt, ...
wchar_t tracepathw[256];
const char* tmppath = getenv("TMP");
int pid = getpid();
- NKDbgPrintfW(L"pid is %d (%x)", pid, pid);
+ NKDbgPrintfW(L"pid is %d (%x)\n", pid, pid);
if (!tmppath || strlen(tmppath) == 0)
sprintf(tracepath, "/Temp/wcetrace%u.log", pid);
else
@@ -181,23 +246,26 @@ WCETRACE(int level, const char *fmt, ...
}
}
-void __WCETraceError(int trace, DWORD error, const char* func)
+void
+__WCETraceError(int trace, DWORD error, const char* func)
{
wchar_t* wbuf;
+ int len;
+ char* buf;
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
error,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(wchar_t*) &wbuf,
- 0, NULL );
+ 0, NULL);
- int len = wcslen(wbuf);
- char* buf = alloca(len);
- wcstombs(buf, wbuf, len+1);
+ len = wcslen(wbuf);
+ buf = alloca(len + 1);
+ wcstombs(buf, wbuf, len + 1);
LocalFree(wbuf);
WCETRACE(trace, "%s failed with error %d: %s", func, (int)error, buf);
printf("%s failed with error %d: %s\n", func, (int)error, buf);
}
-#endif // CE_NOTRACE
+#endif /* CE_NOTRACE */
Index: newlib/libc/sys/wince/sys/wcetrace.h
===================================================================
--- newlib/libc/sys/wince/sys/wcetrace.h (revisão 1199)
+++ newlib/libc/sys/wince/sys/wcetrace.h (revisão 1200)
@@ -32,16 +32,26 @@ extern "C" {
#endif
#ifndef CE_NOTRACE
+
void WCETRACEGETENV(void);
void WCETRACESET(int trace);
int WCETRACEGET(void);
-void WCETRACE(int level, const char *fmt, ...);
+#define WCETRACING(LEVEL) \
+ ((WCETRACE_DEBUGGER_GET() & (LEVEL)) || (WCETRACEGET() & (LEVEL)))
+#define WCETRACE(LEVEL, FMT...) do { \
+ if (WCETRACING(LEVEL)) \
+ __WCETrace(LEVEL, FMT); \
+ } while(0)
+
void WCETRACE_DEBUGGER_SET(int trace);
int WCETRACE_DEBUGGER_GET(void);
void WCETRACECLOSE(void);
+void __WCETrace(int trace, const char * fmt, ...);
void __WCETraceError(int level, unsigned long werr, const char* funct);
#define WCETRACE_ERROR(T, ERR) __WCETraceError(T, ERR, __FUNCTION__)
+
#else
+
#define WCETRACEGETENV() do {} while (0)
#define WCETRACESET(trace) do {} while (0)
#define WCETRACEGET() do {} while (0)
@@ -50,6 +60,8 @@ void __WCETraceError(int level, unsigned
#define WCETRACE_DEBUGGER_GET() do {} while (0)
#define WCETRACECLOSE() do {} while (0)
#define WCETRACE_ERROR(T, ERR) do {} while (0)
+#define WCETRACING(p) (0)
+
#endif
#ifdef __cplusplus
Index: newlib/libc/sys/wince/io.c
===================================================================
--- newlib/libc/sys/wince/io.c (revisão 1199)
+++ newlib/libc/sys/wince/io.c (revisão 1200)
@@ -515,13 +515,14 @@ _write_r(struct _reent *reent, int fd, c
WCETRACE(WCE_IO, "write(%d, %d, %x)", fd, count, _fdtab[fd].hnd);
EnterCriticalSection(&critsect);
-#if 1
- if (fd == 2 || fd == 1)
- {
- const char* out = fd == 2?"stderr: ":"stdout: ";
- WCETRACE(WCE_IO, "%s : %s", out, buf);
+
+ if (WCETRACING(WCE_IO)) {
+ if (fd == 2 || fd == 1) {
+ char fmt[30];
+ snprintf(fmt, 29, "%s : %%.%ds", fd == 2?"stderr":"stdout", count);
+ WCETRACE(WCE_IO, fmt, buf);
+ }
}
-#endif
/* until we can call console stuff inside the PE loader */
if ((!__StdioInited) && (fd >= 0) && (fd <= 2))
Index: ChangeLog.cegcc
===================================================================
--- ChangeLog.cegcc (revisão 1199)
+++ ChangeLog.cegcc (revisão 1200)
@@ -1,3 +1,23 @@
+2008-10-09 Pawel Veselov <[EMAIL PROTECTED]>
+
+ newlib/
+ * libc/sys/wince/trace.c (__trace_closed): New.
+ (get_level_name): New.
+ (WCETRACE): Rename to...
+ (__WCETrace): ... this.
+ (WCETRACESET): Do not change the trace level if the tracing
+ facilities have been closed.
+ (WCETRACECLOSE): Print out a message that the tracing has closed.
+ Reset tracing level to 0 after closing.
+ (__WCETrace): Print out the tracing level along with the message.
+ * libc/sys/wince/sys/wcetrace.h (WCETRACE): Define.
+ (WCETRACE): Rename to ...
+ (__WCETrace): ... this.
+ (WCETRACING): Define.
+ * libc/sys/wince/io.c (_write_r): When tracing out stdio/stderr
+ buffer, send out as many bytes as are in the buffer, instead of
+ using zero termination.
+
2008-10-04 Pawel Veselov <[EMAIL PROTECTED]>
newlib/
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Cegcc-devel mailing list
Cegcc-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/cegcc-devel