]> git.saurik.com Git - apt.git/blobdiff - apt-pkg/contrib/error.cc
Upgraded to eg++ 1.1 and libstdc++2.9
[apt.git] / apt-pkg / contrib / error.cc
index d1ea1b87b110814da63da224017f8297bfe8f16f..bbd081d50650b5e7fc3e41aa93d103557c3afe45 100644 (file)
@@ -1,6 +1,6 @@
 // -*- mode: cpp; mode: fold -*-
 // Description                                                         /*{{{*/
-// $Id: error.cc,v 1.2 1998/07/07 04:17:10 jgg Exp $
+// $Id: error.cc,v 1.5 1998/09/18 02:42:40 jgg Exp $
 /* ######################################################################
    
    Global Erorr Class - Global error mechanism
                                                                        /*}}}*/
 // Include Files                                                       /*{{{*/
 #ifdef __GNUG__
-#pragma implementation "pkglib/error.h"
+#pragma implementation "apt-pkg/error.h"
 #endif 
 
+#include <apt-pkg/error.h>
+
 #include <errno.h>
 #include <stdio.h>
 #include <string.h>
 #include <stdarg.h>
+#include <unistd.h>
 
-#include <pkglib/error.h>
                                                                        /*}}}*/
 
-GlobalError *_error = new GlobalError;
+// Global Error Object                                                 /*{{{*/
+/* If the implementation supports posix threads then the accessor function
+   is compiled to be thread safe otherwise a non-safe version is used. A
+   Per-Thread error object is maintained in much the same manner as libc
+   manages errno */
+#if _POSIX_THREADS == 1
+ #include <pthread.h>
+
+ static pthread_key_t ErrorKey;
+ static void ErrorDestroy(void *Obj) {delete (GlobalError *)Obj;};
+ static void KeyAlloc() {pthread_key_create(&ErrorKey,ErrorDestroy);};
+
+ GlobalError *_GetErrorObj()
+ {
+    static pthread_once_t Once = PTHREAD_ONCE_INIT;
+    pthread_once(&Once,KeyAlloc);
+    
+    void *Res = pthread_getspecific(ErrorKey);
+    if (Res == 0)
+       pthread_setspecific(ErrorKey,Res = new GlobalError);
+    return (GlobalError *)Res;
+ }
+#else
+ GlobalError *_GetErrorObj()
+ {
+    static GlobalError *Obj = new GlobalError;
+    return Obj;
+ }
+#endif
+                                                                       /*}}}*/
 
 // GlobalError::GlobalError - Constructor                              /*{{{*/
 // ---------------------------------------------------------------------
@@ -60,6 +91,32 @@ bool GlobalError::Errno(const char *Function,const char *Description,...)
    
    PendingFlag = true;
 
+   return false;   
+}
+                                                                       /*}}}*/
+// GlobalError::WarningE - Get part of the warn string from errno      /*{{{*/
+// ---------------------------------------------------------------------
+/* Function indicates the stdlib function that failed and Description is
+   a user string that leads the text. Form is:
+     Description - Function (errno: strerror)
+   Carefull of the buffer overrun, sprintf.
+ */
+bool GlobalError::WarningE(const char *Function,const char *Description,...)
+{
+   va_list args;
+   va_start(args,Description);
+
+   // sprintf the description
+   char S[400];
+   vsprintf(S,Description,args);
+   sprintf(S + strlen(S)," - %s (%i %s)",Function,errno,strerror(errno));
+
+   // Put it on the list
+   Item *Itm = new Item;
+   Itm->Text = S;
+   Itm->Error = false;
+   Insert(Itm);
+   
    return false;   
 }
                                                                        /*}}}*/