pretty printing of log

This commit is contained in:
Jacob Parker 2011-11-10 16:56:56 -05:00
parent a859357adb
commit 4ab686a035
8 changed files with 99 additions and 10 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
a.out
db
*.swp

8
database/common.h Normal file
View File

@ -0,0 +1,8 @@
#ifndef _COMMON_H_
#define _COMMON_H_
#include <stdint.h>
#include "nameserver.h"
extern NameServer ns;
#endif

View File

@ -1,7 +1,7 @@
#include "common.h"
#include "db.h"
#include "log.h"
#include "sha1.h"
#include "nameserver.h"
#include <cassert>
#include <cstdlib>
#include <cstdio>
@ -292,3 +292,6 @@ uint64_t PosDb::doStockChange(UPC upc, int32_t delta) {
return log.writeEntry(l);
}
std::vector<std::string> PosDb::toString() {
return log.toString();
}

View File

@ -1,9 +1,8 @@
#ifndef _POS_DB_H_
#define _POS_DB_H_
#include "common.h"
#include "sha1.h"
#include "log.h"
#include "nameserver.h"
#include <stdint.h>
#include <list>
#include <map>
#include <string>
@ -34,6 +33,8 @@ public:
int32_t getStock(UPC upc);
uint64_t doStockChange(UPC upc, int32_t delta);
std::vector<std::string> toString();
private:
void acceptLogEntry(const LogEntry & l);
@ -46,7 +47,6 @@ private:
std::map<uint64_t, LogEntry> serial_to_object;
Log log;
NameServer ns;
};
typedef std::multimap<uint32_t, SHA1Hash>::iterator ath_it;

View File

@ -3,6 +3,7 @@
#include <cstdio>
#include <cstring>
#include <cassert>
#include <sstream>
Log::Log(const char * fn)
: log_name(fn), serial(0)
@ -62,3 +63,63 @@ uint64_t Log::nextSerial()
return serial + 1;
}
std::string logTypeToString (E_OBJ_TYPE type) {
switch(type) {
case ET_TRANS: return "TRANS";
case ET_HASH: return "HASH";
case ET_SALE: return "SALE";
case ET_PRICE: return "PRICE";
case ET_REVERT: return "REVERT";
default: fprintf(stderr, "logTypeToString: unhandled case in switch.\n"); abort();
}
}
template<typename T>
std::string ThingToString (T num) {
std::stringstream ss;
ss << num;
return ss.str();
}
std::vector<std::string> Log::toString()
{
assert(sizeof(time_t) == sizeof(uint64_t));
std::vector<std::string> ans;
ans.reserve(entries.size());
std::list<LogEntry>::iterator it;
for(it = entries.begin(); it != entries.end(); it++) {
char* time = new char[64];
struct tm* tms = localtime((time_t*)&it->ts);
strftime(time, 64, "%c", tms);
std::string out = (std::string)time + "\t"
+ ThingToString<uint64_t> (it->serial) + "\t"
+ logTypeToString(it->type) + "\t";
delete[] time;
switch(it->type) {
case ET_TRANS:
out += ns.get_name(it->Transaction.uid) + "\t"
+ ThingToString<int32_t>(it->Transaction.delta);
break;
case ET_HASH:
out += "hash goes here\t"
+ ns.get_name(it->HashChange.uid) + "\t"
+ ThingToString<bool>(it->HashChange.add);
break;
case ET_SALE:
out += "upc goes here\t"
+ ThingToString<int32_t>(it->StockChange.delta);
break;
case ET_PRICE:
out += "upc goes here\t"
+ ThingToString<int32_t>(it->PriceChange.price);
break;
case ET_REVERT:
out += ThingToString<uint64_t>(it->Revert.revert_serial);
break;
default: fprintf(stderr, "Log: unhandled toString case.\n"); abort();
}
ans.push_back(out);
}
return ans;
}

View File

@ -1,9 +1,10 @@
#ifndef _POS_LOG_H_
#define _POS_LOG_H_
#include "common.h"
#include "sha1.h"
#include <list>
#include <string>
#include <stdint.h>
#include <vector>
enum E_OBJ_TYPE { ET_TRANS, ET_HASH, ET_SALE, ET_PRICE, ET_REVERT };
@ -59,6 +60,9 @@ public:
Log(const char * fn);
uint64_t writeEntry(LogEntry ent);
uint64_t nextSerial();
std::vector<std::string> toString();
std::list<LogEntry>::const_iterator begin();
std::list<LogEntry>::const_iterator end();

View File

@ -4,21 +4,26 @@
std::string NameServer::get_name (uint32_t id) {
std::map<std::string, uint32_t>::iterator it;
for (it = cache.begin(); it != cache.end(); it++) {
if (it->second == id) return it->first;
for(it = cache.begin(); it != cache.end(); it++) {
if(it->second == id) return it->first;
}
return "";
struct passwd* pwd = getpwuid(id);
if (!pwd) {
return "???";
}
cache[pwd->pw_name] = id;
return pwd->pw_name;
}
uint32_t NameServer::get_id (std::string name) {
std::map<std::string, uint32_t>::iterator it = cache.find(name);
if (it != cache.end())
if(it != cache.end())
return it->second;
std::cerr << "Doing LDAP lookup for \"" << name << "\".\n";
struct passwd* pwd = getpwnam(name.c_str());
if (!pwd) {
if(!pwd) {
// TODO: oh fuck???
return 0;
}
@ -27,3 +32,4 @@ uint32_t NameServer::get_id (std::string name) {
return cache[name];
}
NameServer ns;

View File

@ -1,3 +1,4 @@
#include "common.h"
#include "db.h"
#include <iostream>
@ -32,6 +33,11 @@ int main() {
foo.l = 123;
cout << db.getStock(foo) << "\n";
cout << "\n\n\n";
std::vector<std::string> lst = db.toString();
for (std::vector<std::string>::iterator it = lst.begin(); it != lst.end(); it++) {
cout << *it << "\n";
}
return 0;
}