6861{
6863 printf("FileHistory::create_file: event [%s]\n", event_name);
6864
6865
6866
6867
6868
6870 localtime_r(×tamp, &
tm);
6871
6872 char buf[256];
6873 strftime(buf,
sizeof(buf),
"%Y%m%d", &
tm);
6874
6875 std::string filename;
6877 filename += "mhf_";
6879 filename += "_";
6880 filename += buf;
6881 filename += "_";
6883
6884 std::string try_filename = filename + ".dat";
6885
6887 for (int itry=0; itry<10; itry++) {
6888 if (itry > 0) {
6889 char s[256];
6890 sprintf(s, "_%d", rand());
6891 try_filename = filename + s + ".dat";
6892 }
6893
6894 fp = fopen(try_filename.c_str(),
"r");
6896
6898 continue;
6899 }
6900
6901 fp = fopen(try_filename.c_str(),
"w");
6903
6904 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));
6905 continue;
6906 }
6907
6908
6909 break;
6910 }
6911
6913
6914 cm_msg(
MERROR,
"FileHistory::create_file",
"Error: Cannot create file \'%s\' for event \'%s\'", filename.c_str(), event_name);
6916 }
6917
6918 std::string ss;
6919
6920 ss += "version: 2.0\n";
6921 ss += "event_name: ";
6922 ss += event_name;
6923 ss += "\n";
6924 ss += "time: ";
6926 ss += "\n";
6927
6928 int recsize = 0;
6929
6930 ss += "tag: /DWORD 1 4 /timestamp\n";
6931 recsize += 4;
6932
6933 bool padded = false;
6935
6936 bool xdebug = false;
6937
6938 for (
size_t i=0;
i<vars.size();
i++) {
6940 int n_bytes = vars[
i].n_data*tsize;
6941 int xalign = (
offset % tsize);
6942
6943 if (xdebug)
6944 printf(
"tag %zu, tsize %d, n_bytes %d, xalign %d\n",
i, tsize, n_bytes, xalign);
6945
6946#if 0
6947
6948 if (xalign != 0) {
6949 padded = true;
6950 int pad_bytes = tsize - xalign;
6951 assert(pad_bytes > 0);
6952
6953 ss += "tag: ";
6954 ss += "XPAD";
6955 ss += " ";
6957 ss += " ";
6959 ss += " ";
6960 ss += "pad_";
6962 ss += "\n";
6963
6965 recsize += pad_bytes;
6966
6967 assert((
offset % tsize) == 0);
6968 fprintf(stderr, "FIXME: need to debug padding!\n");
6969
6970 }
6971#endif
6972
6973 ss += "tag: ";
6975 ss += " ";
6977 ss += " ";
6979 ss += " ";
6981 ss += "\n";
6982
6983 recsize += n_bytes;
6985 }
6986
6987 ss += "record_size: ";
6989 ss += "\n";
6990
6991
6992 int sslength = ss.length() + 127;
6993
6994 int block = 1024;
6995 int nb = (sslength + block - 1)/block;
6996 int data_offset = block * nb;
6997
6998 ss += "data_offset: ";
7000 ss += "\n";
7001
7002 fprintf(
fp,
"%s", ss.c_str());
7003
7005
7006 if (1 && padded) {
7007 printf("Schema in file %s has padding:\n", try_filename.c_str());
7008 printf("%s", ss.c_str());
7009 }
7010
7011 if (filenamep)
7012 *filenamep = try_filename;
7013
7015}
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)