Here is an updated version.

Thanks!
  Pawel.

On Mon, Oct 6, 2008 at 5:54 PM, Pedro Alves <[EMAIL PROTECTED]> wrote:
> A Tuesday 07 October 2008 01:43:57, Pawel Veselov escreveu:
>> On Mon, Oct 6, 2008 at 5:33 PM, Pedro Alves <[EMAIL PROTECTED]> wrote:
>> > <Warning, quick 1-minute, not a full review.>
>> >
>> > On Tuesday 07 October 2008 00:49:05, Pawel Veselov wrote:
>> >> +#define TRACING(level)  ((WCETRACE_DEBUGGER_GET() & level) || \
>> >
>> > Can you rename TRACING into something else no so generic, please?
>> > Sounds like asking for trouble down the line.
>> >
>> > __WCETRACE_P ?
>> >
>> > __WCETRACING ?
>> >
>> > (_P is a semi-standard suffix for predicate.)
>>
>> Sure, will do.
>>
>> >
>> >> +        (WCETRACEGET() & level))
>> >> +#define WCETRACE(level, fmt...) do { \
>> >> +  if (TRACING(level)) { \
>> >> +      __WCETrace((level), fmt); \
>> >
>> > Missing ## __VA_ARGS__ ?
>>
>> Some trick I probably don't know about... I ran gcc -E, and checked
>> that "fmt" expands to the format and the
>> arguments thereafter... I googled this, and It seems that
>>
>> #define M(x...) { m(x); }
>> is the same as
>> #define M(x, ...) { m(x, ##__VA_ARGS__); }
>
> Ah, I missed that.  You version is fine.
>
>>
>> >
>> >> +  } } while(0)
>> >> +
>> >
>> > --
>> > Pedro Alves
>> >
>>
>> Thanks,
>>  Pawel.
>>
>>
>
>
>
> --
> Pedro Alves
>



-- 
With best of best regards
Pawel S. Veselov
Index: src/newlib/newlib/libc/sys/wince/trace.c
===================================================================
--- src/newlib/newlib/libc/sys/wince/trace.c	(revision 1198)
+++ src/newlib/newlib/libc/sys/wince/trace.c	(working copy)
@@ -15,11 +15,15 @@
 static HANDLE __wcetracehnd = NULL;
 static int __wcetrace = 0;
 static int __wcetrace_debugger = 0;	/* Used to be WCE_ALL */
+static int __trace_closed = 0;
+static void get_level_name(int, char *);
 
 void
 WCETRACESET(int trace)
 {
-  __wcetrace = trace;
+    if (!__trace_closed) {
+      __wcetrace = trace;
+    }
 }
 
 int
@@ -46,19 +50,29 @@ 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 counted, 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 }
 };
@@ -122,22 +136,31 @@ WCETRACECLOSE()
 {
   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, ...)
 {
   if (!(__wcetrace_debugger & level) && !(__wcetrace & level))
     return;
 
+  int len;
+  char level_name[MAX_LEVEL_BUF];
   char buf[1024];
-  int len = sprintf(buf, "%08X:%08X: ", GetTickCount(), GetCurrentThreadId());
 
   va_list ap;
+
+  get_level_name(level, level_name);
+  len = sprintf(buf, "%08X:%08X: [%s] ", GetTickCount(), GetCurrentThreadId(), level_name);
+
   va_start(ap, fmt);
   vsprintf(buf + len, fmt, ap);
   strcat(buf, "\n");
@@ -148,7 +171,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 
@@ -200,4 +223,38 @@ void __WCETraceError(int trace, DWORD er
   printf("%s failed with error %d: %s\n", func, (int)error, buf); 
 }
 
-#endif // CE_NOTRACE
+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 str_len;
+            level &= ~ent->flag;
+            if (!virgin) {
+                *(to++) = ',';
+            } else {
+                virgin = 0;
+            }
+            str_len = strlen(ent->str);
+            memcpy(to, ent->str, str_len);
+            to += str_len;
+        }
+    }
+
+    if (level) {
+        int printed;
+        /* there were some unresolved flags */
+        if (!virgin) {
+            *(to++) = ',';
+        }
+        printed = sprintf(to, "%#04.4x", level);
+        to += printed;
+    }
+
+    *to = 0;
+
+}
+
+#endif /* CE_NOTRACE */
Index: src/newlib/newlib/libc/sys/wince/sys/wcetrace.h
===================================================================
--- src/newlib/newlib/libc/sys/wince/sys/wcetrace.h	(revision 1198)
+++ src/newlib/newlib/libc/sys/wince/sys/wcetrace.h	(working copy)
@@ -32,13 +32,21 @@ 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
@@ -50,6 +58,7 @@ 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: src/newlib/newlib/libc/sys/wince/io.c
===================================================================
--- src/newlib/newlib/libc/sys/wince/io.c	(revision 1198)
+++ src/newlib/newlib/libc/sys/wince/io.c	(working copy)
@@ -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: src/newlib/ChangeLog.cegcc
===================================================================
--- src/newlib/ChangeLog.cegcc	(revision 1198)
+++ src/newlib/ChangeLog.cegcc	(working copy)
@@ -1,3 +1,23 @@
+2008-10-06  Pawel Veselov  <[EMAIL PROTECTED]>
+
+	newlib/
+	* libc/sys/wince/trace.c : Defined __trace_closed to check for when
+	tracing has been closed.  Define and implement get_level_name() to
+	convert trace level into a string.  Made level variables static so
+	outside code can not change them.  Renamed WCETRACE to __WCETrace.
+	* libc/sys/wince/trace.c (WCETRACESET) : do not cange the trace level
+	if the tracing facilities have been closed.
+	* libc/sys/wince/trace.c (WCETRACECLOSE) : print out a message that
+	the tracing has closed. Reset tracing level to 0 after closing.
+	* libc/sys/wince/trace.c (__WCETrace) : print out the tracing level
+	along with the message.
+	* libc/sys/wince/sys/wcetrace.h : Define WCETRACE define, renamed old
+	WCETRACE function to __WCETrace.  Define WCETRACING that checks for
+        whether tracing is enabled for the specified level.
+	* 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

Reply via email to