status.cc

00001 /* 
00002    EyeDB Object Database Management System
00003    Copyright (C) 1994-2008 SYSRA
00004    
00005    EyeDB is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009    
00010    EyeDB is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014    
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with this library; if not, write to the Free Software
00017    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA 
00018 */
00019 
00020 /*
00021    Author: Eric Viara <viara@sysra.com>
00022 */
00023 
00024 #include <eyedbconfig.h>
00025 
00026 #include <string.h>
00027 
00028 #include <eyedbsm/eyedbsm.h>
00029 #include <eyedblib/log.h>
00030 #include <pthread.h>
00031 #include <eyedblib/butils.h>
00032 #include <stdlib.h>
00033 #include <stdarg.h>
00034 #include <eyedblib/m_mem.h>
00035 #include "lib/m_mem_p.h"
00036 
00037 /*extern char *eyedblib::getFBuffer(const char *fmt, va_list ap);*/
00038 
00039 #define NSTATUS 8
00040 
00041 namespace eyedbsm {
00042 
00043   static int w_status;
00044 
00045   struct StatusContext {
00046     StatusRec status;
00047     char *err_msg;
00048     int err_msg_len;
00049   };
00050 
00051   static StatusContext statusCtx[NSTATUS];
00052 
00053   static StatusContext *
00054   getStatusContext(int *pnstatus)
00055   {
00056     if (w_status >= NSTATUS)
00057       w_status = 0;
00058     if (pnstatus)
00059       *pnstatus = w_status;
00060     return &statusCtx[w_status++];
00061   }
00062 
00063   Status
00064   statusMake(Error err, const char *fmt, ...)
00065   {
00066     va_list ap;
00067     StatusContext *s;
00068     int nstatus;
00069     char *buf;
00070 
00071     va_start(ap, fmt);
00072     buf = eyedblib::getFBuffer(fmt, ap);
00073     va_end(ap);
00074 
00075     va_start(ap, fmt);
00076     vsprintf(buf, fmt, ap);
00077     va_end(ap);
00078 
00079     IDB_LOG(IDB_LOG_EXCEPTION, ("%s\n", buf));
00080 
00081     s = getStatusContext(&nstatus);
00082 
00083     if (strlen(buf) >= s->err_msg_len)
00084       {
00085         s->err_msg_len = strlen(buf)+10;
00086         s->err_msg = (char *)m_realloc(s->err_msg, s->err_msg_len);
00087       }
00088 
00089     s->status.err = err;
00090     s->status.err_msg = s->err_msg;
00091     strcpy(s->status.err_msg, buf);
00092 
00093     nstatus++;
00094 
00095     return &s->status;
00096   }
00097 
00098   Status
00099   statusMake_s(Error err)
00100   {
00101     StatusContext *s;
00102 
00103     s = getStatusContext(0);
00104 
00105     s->status.err = err;
00106     s->status.err_msg = "";
00107 
00108     return &s->status;
00109   }
00110 
00111   static char *_seError[N_ERROR];
00112 
00113 #define PREFIX "storage manager: "
00114 
00115   void
00116   errorInit(void)
00117   {
00118     if (!_seError[SUCCESS])
00119       {
00120         _seError[SUCCESS] =
00121           PREFIX "success";
00122         _seError[ERROR] =
00123           PREFIX "error";
00124         _seError[SYS_ERROR] =
00125           PREFIX "system error";
00126         _seError[CONNECTION_FAILURE] =
00127           PREFIX "connection failure";
00128         _seError[SERVER_FAILURE] =
00129           PREFIX "server failure";
00130         _seError[CANNOT_LOCK_SHMFILE] =
00131           PREFIX "cannot lock shm file";
00132         _seError[DB_ALREADY_LOCK_BY_A_SERVER] =
00133           PREFIX "db already lock by a server";
00134         _seError[INVALID_DBID] =
00135           PREFIX "invalid dbid";
00136         _seError[INVALID_MAXSIZE] =
00137           PREFIX "invalid maxsize";
00138         _seError[INVALID_SIZESLOT] =
00139           PREFIX "invalid sizeslot";
00140         _seError[INVALID_NBSLOTS] =
00141           PREFIX "invalid slot number";
00142         _seError[INVALID_NBOBJS] =
00143           PREFIX "invalid object number";
00144         _seError[DATABASE_CREATION_ERROR] =
00145           PREFIX "database creation error";
00146         _seError[DATABASE_ACCESS_DENIED] =
00147           PREFIX "database access denied";
00148         _seError[DATABASE_OPEN_FAILED] =
00149           PREFIX "database open failed";
00150         _seError[INVALID_DATAFILE_CNT] =
00151           PREFIX "invalid datafile count";
00152         _seError[INVALID_DATASPACE_CNT] =
00153           PREFIX "invalid dataspace count";
00154         _seError[INVALID_DATAFILE_CNT_IN_DATASPACE] =
00155           PREFIX "invalid datafile count in a dataspace";
00156         _seError[INVALID_DBFILE] =
00157           PREFIX "invalid database file";
00158         _seError[INVALID_DBFILE_ACCESS] =
00159           PREFIX "invalid database file access";
00160         _seError[INVALID_SHMFILE] =
00161           PREFIX "invalid shm file";
00162         _seError[INVALID_SHMFILE_ACCESS] =
00163           PREFIX "invalid shm file access";
00164         _seError[INVALID_OBJMAP_ACCESS] =
00165           PREFIX "invalid oid map file access";
00166         _seError[INVALID_DATAFILE] =
00167           PREFIX "invalid datafile";
00168         _seError[INVALID_DATASPACE] =
00169           PREFIX "invalid dataspace";
00170         _seError[INVALID_DMPFILE] =
00171           PREFIX "invalid data map file";
00172         _seError[INVALID_DATAFILEMAXSIZE] =
00173           PREFIX "invalid datafile maxsize";
00174         _seError[INVALID_FILES_COPY] =
00175           PREFIX "invalid files copy";
00176         _seError[INVALID_DBFILES_COPY] =
00177           PREFIX "invalid database files copy";
00178         _seError[INVALID_DATAFILES_COPY] =
00179           PREFIX "invalid data files copy";
00180         _seError[INVALID_SHMFILES_COPY] =
00181           PREFIX "invalid shm files copy";
00182         _seError[INVALID_OBJMAPFILES_COPY] =
00183           PREFIX "invalid object map files copy";
00184         _seError[DBFILES_IDENTICAL] =
00185           PREFIX "database files are identical";
00186         _seError[DATAFILES_IDENTICAL] =
00187           PREFIX "data files are identical";
00188         _seError[DBFILE_ALREADY_EXISTS] =
00189           PREFIX "database file already exists";
00190         _seError[SHMFILE_ALREADY_EXISTS] =
00191           PREFIX "shm file already exists";
00192         _seError[OBJMAPFILE_ALREADY_EXISTS] =
00193           PREFIX "object map file already exists";
00194         _seError[DATAFILE_ALREADY_EXISTS] =
00195           PREFIX "data file already exists";
00196         _seError[SIZE_TOO_LARGE] =
00197           PREFIX "size too large";
00198         _seError[WRITE_FORBIDDEN] =
00199           PREFIX "write forbidden";
00200         _seError[BACKEND_INTERRUPTED] =
00201           PREFIX "backend interrupted";
00202         _seError[CONN_RESET_BY_PEER] =
00203           PREFIX "connection reset by peer";
00204         _seError[FATAL_MUTEX_LOCK_TIMEOUT] =
00205           PREFIX "fatal mutex lock timeout: the shmem must be cleanup "
00206           "(or possibly the computer is overloaded)";
00207         _seError[LOCK_TIMEOUT] =
00208           PREFIX "lock timeout";
00209         _seError[INVALID_FLAG] =
00210           PREFIX "invalid flag";
00211         _seError[INVALID_DB_HANDLE] =
00212           PREFIX "invalid database handle";
00213         _seError[TRANSACTION_TOO_MANY_NESTED] =
00214           PREFIX "too many transactions nested";
00215         _seError[TOO_MANY_TRANSACTIONS] =
00216           PREFIX "too many transactions";
00217         _seError[TRANSACTION_NEEDED] =
00218           PREFIX "transaction needed";
00219         _seError[TRANSACTION_LOCKING_FAILED] =
00220           PREFIX "transaction locking failed";
00221         _seError[TRANSACTION_UNLOCKING_FAILED] =
00222           PREFIX "transaction unlocking failed";
00223         _seError[DEADLOCK_DETECTED] =
00224           PREFIX "deadlock detected";
00225         _seError[INVALID_TRANSACTION_MODE] =
00226           PREFIX "invalid transaction mode";
00227         _seError[RW_TRANSACTION_NEEDED] =
00228           PREFIX "read write mode transaction needed";
00229         _seError[NOT_YET_IMPLEMENTED] =
00230           PREFIX "not yet implemented";
00231         _seError[MAP_ERROR] =
00232           PREFIX "map error";
00233         _seError[INVALID_MAPTYPE] =
00234           PREFIX "invalid map type";
00235         _seError[INVALID_OBJECT_SIZE] =
00236           PREFIX "invalid object size";
00237         _seError[INVALID_OFFSET] =
00238           PREFIX "invalid object offset";
00239         _seError[TOO_MANY_OBJECTS] =
00240           PREFIX "maximum object count has been reached";
00241         _seError[NO_DATAFILESPACE_LEFT] =
00242           PREFIX "no space left on dataspace";
00243         _seError[NO_SHMSPACE_LEFT] =
00244           PREFIX "no space left on shm";
00245         _seError[INVALID_SIZE] =
00246           PREFIX "invalid size";
00247         _seError[PROTECTION_INVALID_UID] =
00248           PREFIX "protection invalid uid";
00249         _seError[PROTECTION_DUPLICATE_UID] =
00250           PREFIX "protection duplicate uid";
00251         _seError[PROTECTION_DUPLICATE_NAME] =
00252           PREFIX "protection duplicate name";
00253         _seError[PROTECTION_NOT_FOUND] =
00254           PREFIX "protection not found";
00255         _seError[INVALID_OID] =
00256           PREFIX "invalid oid";
00257         _seError[OBJECT_PROTECTED] =
00258           PREFIX "object protected";
00259         _seError[INVALID_ROOT_ENTRY_SIZE] =
00260           PREFIX "invalid root entry size";
00261         _seError[INVALID_ROOT_ENTRY_KEY] =
00262           PREFIX "invalid root entry key";
00263         _seError[INVALID_READ_ACCESS] =
00264           PREFIX "invalid read access";
00265         _seError[INVALID_WRITE_ACCESS] =
00266           PREFIX "invalid write access";
00267         _seError[PROT_NAME_TOO_LONG] =
00268           PREFIX "prot name too long";
00269         _seError[ROOT_ENTRY_EXISTS] =
00270           PREFIX "root entry exists";
00271         _seError[TOO_MANY_ROOT_ENTRIES] =
00272           PREFIX "too many root entries";
00273         _seError[ROOT_ENTRY_NOT_FOUND] =
00274           PREFIX "root entry not found";
00275         _seError[NOTIMPLEMENTED] =
00276           PREFIX "notimplemented";
00277         _seError[NO_SETUID_PRIVILEGE] =
00278           PREFIX "no setuid privilege";
00279         _seError[COMPATIBILITY_ERROR] =
00280           PREFIX "compatibility error";
00281         _seError[INTERNAL_ERROR] =
00282           PREFIX "internal error";
00283         _seError[FATAL_ERROR] =
00284           PREFIX "fatal error";
00285 
00286 #if 1
00287         {
00288           int i;
00289           for (i = 0; i < N_ERROR; i++)
00290             if (!_seError[i])
00291               fprintf(stderr, "_seError not set for %d\n", i);
00292         }
00293 #endif
00294       }
00295   }
00296 
00297   const char *
00298   statusGet(Status status)
00299   {
00300     if (status != Success)
00301       {
00302         const char *s = status->err_msg;
00303         errorInit();
00304 
00305         if (s && *s)
00306           {
00307             static int buf_len;
00308             static char *buf;
00309             int len = strlen(_seError[status->err]) + strlen(s) + 12;
00310             if (len >= buf_len) {
00311               buf_len = len + 256;
00312               free(buf);
00313               buf = (char *)m_malloc(buf_len);
00314             }
00315             sprintf(buf, "%s: %s", _seError[status->err], s);
00316             return buf;
00317           }
00318         else
00319           return _seError[status->err];
00320       }
00321 
00322     return "";
00323   }
00324 
00325   const char *
00326   statusGet_err(int err)
00327   {
00328     if (err != SUCCESS)
00329       {
00330         errorInit();
00331         if (err >= SUCCESS && err < N_ERROR)
00332           return _seError[err];
00333         else
00334           return "";
00335       }
00336 
00337     return "";
00338   }
00339 
00340   Status
00341   statusPrint(Status status, const char *fmt, ...)
00342   {
00343     va_list ap;
00344 
00345     va_start(ap, fmt);
00346 
00347     if (status != Success)
00348       {
00349         const char *s = status->err_msg;
00350         char *buf;
00351 
00352         errorInit();
00353 
00354         buf = eyedblib::getFBuffer(fmt, ap);
00355         vsprintf(buf, fmt, ap);
00356 
00357         fprintf(stderr, "%s: ", _seError[status->err]);
00358         if (*buf)
00359           {
00360             fprintf(stderr, "%s: ", buf);
00361 
00362             if (s && *s)
00363               fprintf(stderr, "%s\n", s);
00364           }
00365         else
00366           {
00367             if (s && *s)
00368               fprintf(stderr, ": %s", s);
00369             fprintf(stderr, "\n");
00370           }
00371 
00372         fflush(stderr);
00373       }
00374 
00375     va_end(ap);
00376     return status;
00377   }
00378 }

Generated on Mon Dec 22 18:16:08 2008 for eyedb by  doxygen 1.5.3