Skip to content

Instantly share code, notes, and snippets.

@bradmontgomery
Created April 30, 2018 20:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bradmontgomery/7187b876039feb64717931166bd6e58d to your computer and use it in GitHub Desktop.
Save bradmontgomery/7187b876039feb64717931166bd6e58d to your computer and use it in GitHub Desktop.
An experiment in file writing.
/**
*
* Experiment: Trying to see how an output file could possibly be corrupted
* by failed program....
*
*
* e.g. https://trello.com/c/7F9VWd2s/980-error-in-vincsv-from-the-logger
*
* Experiment:
*
* 1. Start multiple processes: ./test a, ./test b
* 2. Let them run for a couple of seconds.
* 3. Kill process b, then kill process a.
* 4. The output file will have data from both processes.
*
*/
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <unistd.h>
unsigned int RECORDS = 100;
unsigned int SLEEP = 100000; // in microseconds
using namespace std;
void output(ofstream *fs, string value, int i, string process) {
ostringstream ss;
ss << process << ": [[ " << value << " / " << value << ": " << i << " ]]" << endl;
*fs << ss.str();
fs->flush();
}
int main(int argc, char *argv[]) {
if(argc != 2) {
cout << "USAGE: ./test <foo>" << endl;
return 1;
}
int i = 0;
string proc = argv[1];
ofstream fs;
string filename = "output.txt";
cout << "Starting..." << endl;
fs.open(filename.c_str(), fstream::out);
cout << "Opened file: " << filename.c_str() << endl;
while(i < RECORDS) {
output(&fs, "testing", i, proc);
cout << "Wrote record " << i << endl;
i++;
usleep(SLEEP);
}
fs.close();
cout << "...done." << endl;
return 0;
}
@rmwoods
Copy link

rmwoods commented Apr 30, 2018

So you're thinking the logger is running multiple instances and thus writing to the file multiple times?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment