Ignore:
Timestamp:
2011-08-04T10:24:33+12:00 (13 years ago)
Author:
jmt12
Message:

Explicitly include cstdlib as modern compilers don't add by default

File:
1 edited

Legend:

Unmodified
Added
Removed
  • gs2-extensions/tdb-edit/trunk/src/src/txt2tdb-src/txt2tdb.cpp

    r24045 r24365  
    3333#include <iostream>
    3434#endif
     35
     36#include <cstdlib>
     37
     38#include <time.h>
    3539
    3640#include "tdb.h"
     
    4650#endif
    4751
     52/**
     53 */
    4854void
    4955printUsage (char *program_name)
    5056{
    51   cerr << "usage: " << program_name << " [options] database-name" << endl << endl;
     57  cerr << "usage: " << program_name << " [-append] database-name [-debug]" << endl << endl;
    5258  cerr << "options:" << endl;
    53   cerr << " -append        append to existing database" << endl << endl;
     59  cerr << " -append        append to existing database" << endl;
     60  cerr << " -debug         add timing information to database" << endl << endl;
    5461}
    5562/** printUsage() **/
    5663
     64void
     65debugLog(TDB_CONTEXT * tdb, char * msg_content)
     66{
     67  // Since this log will be used to track order of events, we need an indicator
     68  // of time
     69  time_t seconds = time(NULL);
     70  // We also need some idea of what thread this is - let's try and use the PID
     71  pid_t process_id = getpid();
     72  // Append the message to the entry in the db (fixed key "debuglog")
     73  TDB_DATA key_datum;
     74  key_datum.dptr = (unsigned char *)"debuglog";
     75  key_datum.dsize = 8;
     76  text_t message = "[" + text_t(seconds) + "][" + text_t(process_id) + "] " + msg_content + "\n";
     77  TDB_DATA msg_datum;
     78  msg_datum.dptr = (unsigned char *) message.getcstr();
     79  msg_datum.dsize = message.size();
     80  if (tdb_append(tdb, key_datum, msg_datum) != 0)
     81  {
     82    cerr << "txt2tdb::debugLog() - tdb_append returned an error" << endl;
     83    exit (0);
     84  }
     85}
     86/** debugLog() **/
     87
     88/**
     89 */
    5790int
    5891main (int argc, char *argv[])
    5992{
    6093  // sanity check
    61   if (argc != 2 && argc != 3)
     94  if (2 > argc || argc > 4)
    6295  {
    6396    printUsage (argv[0]);
     
    68101  int append = 0;
    69102  int delkey = 0;
     103  int debug = 0;
    70104  if (argc == 3)
    71105  {
     
    74108      append = 1;
    75109      dbname = argv[2];
     110    }
     111    else if (strcmp(argv[2], "-debug") == 0)
     112    {
     113      dbname = argv[1];
     114      debug = 1;
     115    }
     116    else
     117    {
     118      cerr << argv[1] << " is not a valid option." << endl << endl;
     119      printUsage(argv[0]);
     120      exit (0);
     121    }
     122  }
     123  else if (argc == 4)
     124  {
     125    if (strcmp (argv[1], "-append") == 0 && strcmp (argv[3], "-debug") == 0)
     126    {
     127      append = 1;
     128      dbname = argv[2];
     129      debug = 1;
    76130    }
    77131    else
     
    94148    tdb_flags = TDB_CLEAR_IF_FIRST;
    95149  }
     150  // Disable file IO for testing purposes
     151  /*tdb_flags = tdb_flags | TDB_INTERNAL;*/
     152
    96153  int tdb_store_flags = TDB_DEFAULT; // used later when storing
    97154  int open_flags = O_RDWR | O_CREAT;
     
    99156  if (!tdb)
    100157  {
    101     cerr << "couldn't create " << dbname << endl;
     158    cerr << "txt2tdb::main() - couldn't create " << dbname << endl;
    102159    exit (0);
     160  }
     161
     162  // If we are debugging, we'll write that we just opened the connection
     163  if (debug)
     164  {
     165    debugLog(tdb, "opened connection to database for read/write");
    103166  }
    104167
     
    226289  }
    227290
     291  // If we are debugging, we'll write that we are about to close the connection
     292  if (debug)
     293  {
     294    debugLog(tdb, "closing connection to database");
     295  }
     296
    228297  // Close the database connection
    229298  if (tdb_close(tdb) < 0)
Note: See TracChangeset for help on using the changeset viewer.