Added silc_errno_set_location and silc_errno_location
authorPekka Riikonen <priikone@silcnet.org>
Sat, 21 Jun 2008 12:06:08 +0000 (15:06 +0300)
committerPekka Riikonen <priikone@silcnet.org>
Sat, 21 Jun 2008 12:06:08 +0000 (15:06 +0300)
They can be used to set and retrieve the location of error in a
file.

lib/silcutil/silcerrno.c
lib/silcutil/silcerrno.h
lib/silcutil/silcthread_i.h

index c8245c647e6b330fc127d219e21fcb41b8f153da..ae7af839add398c7b3e0f03e36e10af30110aa0c 100644 (file)
@@ -45,6 +45,7 @@ void silc_set_errno(SilcResult error)
   }
 
   tls->error_reason[0] = '\0';
+  tls->filename[0] = '\0';
   tls->error = error;
 }
 
@@ -58,6 +59,7 @@ void silc_set_errno_nofail(SilcResult error)
     return;
 
   tls->error_reason[0] = '\0';
+  tls->filename[0] = '\0';
   tls->error = error;
 }
 
@@ -75,9 +77,11 @@ void silc_set_errno_reason(SilcResult error, const char *format, ...)
       return;
   }
 
-  va_start(va, format);
-  silc_vsnprintf(tls->error_reason, sizeof(tls->error_reason), format, va);
-  va_end(va);
+  if (format) {
+    va_start(va, format);
+    silc_vsnprintf(tls->error_reason, sizeof(tls->error_reason), format, va);
+    va_end(va);
+  }
 
   tls->error = error;
 }
@@ -92,9 +96,11 @@ void silc_set_errno_reason_nofail(SilcResult error, const char *format, ...)
   if (!tls)
     return;
 
-  va_start(va, format);
-  silc_vsnprintf(tls->error_reason, sizeof(tls->error_reason), format, va);
-  va_end(va);
+  if (format) {
+    va_start(va, format);
+    silc_vsnprintf(tls->error_reason, sizeof(tls->error_reason), format, va);
+    va_end(va);
+  }
 
   tls->error = error;
 }
@@ -435,3 +441,43 @@ const char *silc_errno_string(SilcResult error)
 
   return silc_errno_strings[error];
 }
+
+/* Set error location */
+
+void silc_set_errno_location(const char *filename,
+                            SilcUInt32 current_line_number,
+                            SilcUInt32 current_column_number)
+{
+  SilcTls tls = silc_thread_get_tls();
+
+  if (!tls)
+    return;
+
+  if (filename)
+    silc_snprintf(tls->filename, sizeof(tls->filename), filename);
+  tls->cur_line = current_line_number;
+  tls->cur_column = current_column_number;
+}
+
+/* Get error location */
+
+SilcBool silc_errno_location(const char **filename,
+                            SilcUInt32 *current_line,
+                            SilcUInt32 *current_column)
+{
+  SilcTls tls = silc_thread_get_tls();
+
+  if (!tls)
+    return FALSE;
+  if (tls->filename[0] == '\0' && !tls->cur_line && !tls->cur_column)
+    return FALSE;
+
+  if (filename)
+    *filename = tls->filename;
+  if (current_line)
+    *current_line = tls->cur_line;
+  if (current_column)
+    *current_column = tls->cur_column;
+
+  return TRUE;
+}
index c00e9af02c3bd2d24fb7ca77251f10be022107fd..cf5210f97d359d8bb1f5cb95fd0877ec7d38041b 100644 (file)
@@ -183,6 +183,28 @@ const char *silc_errno_string(SilcResult error);
  ***/
 const char *silc_errno_reason(void);
 
+/****d* silcutil/silc_errno_location
+ *
+ * NAME
+ *
+ *    SilcBool silc_errno_location(const char **filename,
+ *                                 SilcUInt32 *current_line,
+ *                                 SilcUInt32 *current_column);
+ *
+ * DESCRIPTION
+ *
+ *    Returns the error location information.  The filename where the error
+ *    occurred is returned to `filename'.  The line and columns where the
+ *    error occurred is returned to `current_line' and `current_column',
+ *    respectively, or 0 is returned if the information is not present.
+ *
+ *    Returns FALSE if the location information is not available.
+ *
+ ***/
+SilcBool silc_errno_location(const char **filename,
+                            SilcUInt32 *current_line,
+                            SilcUInt32 *current_column);
+
 /* Low-level routines for the error handling. */
 
 /****d* silcutil/silc_set_errno
@@ -254,6 +276,27 @@ void silc_set_errno_reason_nofail(SilcResult error, const char *format, ...);
  ***/
 void silc_set_errno_posix(int error);
 
+/****d* silcutil/silc_set_errno_location
+ *
+ * NAME
+ *
+ *    void silc_set_errno_location(const char *filename,
+ *                                 SilcUInt32 current_line_number,
+ *                                 SilcUInt32 current_column_number);
+ *
+ * DESCRIPTION
+ *
+ *    A low level routine to set the error location in a file indicated by
+ *    the `filename'.  The `current_line_number' is the line where the
+ *    error occurred.  The `current_column_number' number if the column in
+ *    the line where the error occurred or 0 if so such information is
+ *    present.  The errno must be set before calling this function.
+ *
+ ***/
+void silc_set_errno_location(const char *filename,
+                            SilcUInt32 current_line_number,
+                            SilcUInt32 current_column_number);
+
 /* Return last error */
 SilcResult silc_get_errno(void);
 
index f7c6697f1b2be45e4cb6d0a8e3017d739811f8f4..e0bb7a238c72578e640daf947e7210dea450e992 100644 (file)
@@ -37,7 +37,10 @@ typedef struct SilcTlsObject {
   void *thread_context;                            /* Context set with SILC Tls API */
   void *platform_context;                  /* Platform specific context */
   char error_reason[256];                  /* Reason for the error */
+  char filename[256];                      /* File where error occurred */
   SilcResult error;                        /* Errno, last error */
+  SilcUInt32 cur_line;                     /* Line number of error */
+  SilcUInt32 cur_column;                   /* Column number of error */
   unsigned int shared_data     : 1;        /* Set when shares data with other
                                               threads in the Tls. */
 } *SilcTls, SilcTlsStruct;