6873{
6875 printf("FileHistory::create_file: event [%s]\n", event_name);
6876
6877
6878
6879
6880
6882 localtime_r(×tamp, &
tm);
6883
6884 char buf[256];
6885 strftime(buf,
sizeof(buf),
"%Y%m%d", &
tm);
6886
6887 std::string filename;
6889 filename += "mhf_";
6891 filename += "_";
6892 filename += buf;
6893 filename += "_";
6895
6896 std::string try_filename = filename + ".dat";
6897
6899 for (int itry=0; itry<10; itry++) {
6900 if (itry > 0) {
6901 char s[256];
6902 sprintf(s, "_%d", rand());
6903 try_filename = filename + s + ".dat";
6904 }
6905
6906 fp = fopen(try_filename.c_str(),
"r");
6908
6910 continue;
6911 }
6912
6913 fp = fopen(try_filename.c_str(),
"w");
6915
6916 cm_msg(
MERROR,
"FileHistory::create_file",
"Error: Cannot create file \'%s\' for event \'%s\', fopen() errno %d (%s)", try_filename.c_str(), event_name, errno, strerror(errno));
6917 continue;
6918 }
6919
6920
6921 break;
6922 }
6923
6925
6926 cm_msg(
MERROR,
"FileHistory::create_file",
"Error: Cannot create file \'%s\' for event \'%s\'", filename.c_str(), event_name);
6928 }
6929
6930 std::string ss;
6931
6932 ss += "version: 2.0\n";
6933 ss += "event_name: ";
6934 ss += event_name;
6935 ss += "\n";
6936 ss += "time: ";
6938 ss += "\n";
6939
6940 int recsize = 0;
6941
6942 ss += "tag: /DWORD 1 4 /timestamp\n";
6943 recsize += 4;
6944
6945 bool padded = false;
6947
6948 bool xdebug = false;
6949
6950 for (
size_t i=0;
i<vars.size();
i++) {
6952 int n_bytes = vars[
i].n_data*tsize;
6953 int xalign = (
offset % tsize);
6954
6955 if (xdebug)
6956 printf(
"tag %zu, tsize %d, n_bytes %d, xalign %d\n",
i, tsize, n_bytes, xalign);
6957
6958#if 0
6959
6960 if (xalign != 0) {
6961 padded = true;
6962 int pad_bytes = tsize - xalign;
6963 assert(pad_bytes > 0);
6964
6965 ss += "tag: ";
6966 ss += "XPAD";
6967 ss += " ";
6969 ss += " ";
6971 ss += " ";
6972 ss += "pad_";
6974 ss += "\n";
6975
6977 recsize += pad_bytes;
6978
6979 assert((
offset % tsize) == 0);
6980 fprintf(stderr, "FIXME: need to debug padding!\n");
6981
6982 }
6983#endif
6984
6985 ss += "tag: ";
6987 ss += " ";
6989 ss += " ";
6991 ss += " ";
6993 ss += "\n";
6994
6995 recsize += n_bytes;
6997 }
6998
6999 ss += "record_size: ";
7001 ss += "\n";
7002
7003
7004 int sslength = ss.length() + 127;
7005
7006 int block = 1024;
7007 int nb = (sslength + block - 1)/block;
7008 int data_offset = block * nb;
7009
7010 ss += "data_offset: ";
7012 ss += "\n";
7013
7014 fprintf(
fp,
"%s", ss.c_str());
7015
7017
7018 if (1 && padded) {
7019 printf("Schema in file %s has padding:\n", try_filename.c_str());
7020 printf("%s", ss.c_str());
7021 }
7022
7023 if (filenamep)
7024 *filenamep = try_filename;
7025
7027}
char type[NAME_LENGTH]
history channel name
INT cm_msg(INT message_type, const char *filename, INT line, const char *routine, const char *format,...)
const char * rpc_tid_name(INT id)
static std::string TimeToString(time_t t)
static std::string SmallIntToString(int i)
static std::string MidasNameToFileName(const char *s)