Skip to content

Commit

Permalink
Handle empty files better.
Browse files Browse the repository at this point in the history
  • Loading branch information
dustin committed Jun 1, 2009
1 parent adb637f commit caba7be
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
8 changes: 5 additions & 3 deletions logfiles.cpp
Expand Up @@ -44,13 +44,13 @@ int LogFile::openLogfile()
std::ios_base::in | std::ios_base::binary);
assert(file != NULL);
if (file->fail()) {
throw std::runtime_error("Error opening logfile.");
throw LogfileError("Error opening logfile.");
}
assert(file->is_open());

bool isgzip = file->get() == 037 && file->get() == 0213;
if (file->fail()) {
throw std::runtime_error("Error trying to read magic");
throw LogfileError("Error trying to read magic");
}
file->seekg(0, std::ios_base::beg);

Expand Down Expand Up @@ -263,7 +263,9 @@ LogFile::~LogFile()
closeLogfile();
}

delete outputter;
if (outputter != NULL) {
delete outputter;
}

/* Free the parts */
if(filename!=NULL) {
Expand Down
14 changes: 14 additions & 0 deletions logfiles.h
Expand Up @@ -45,6 +45,20 @@ class S3LineOutputter : public LineOutputter {
void writeLine(std::string s);
};

class LogfileError : public std::exception {
public:
LogfileError(const char *s) {
msg = s;
};

virtual const char *what() const throw() {
return msg;
}

private:
const char *msg;
};

/* The logfile itself */
class LogFile {
public:
Expand Down
9 changes: 7 additions & 2 deletions logmerge.cpp
Expand Up @@ -23,8 +23,13 @@ namespace logmerge {
}

static void logmerge::initLogfile(log_queue& queue, const char *filename) {
LogFile *lf=new LogFile(filename);
queue.push(lf);
try {
LogFile *lf=new LogFile(filename);
queue.push(lf);
} catch(LogfileError e) {
std::cerr << "*** Error initializing ``" << filename << "'': " << e.what()
<< std::endl;
}
}

/* Initialize all of the logfiles */
Expand Down
Empty file added samples/empty_file
Empty file.

0 comments on commit caba7be

Please sign in to comment.