Hej!
Cześć mego kodu działającego jako proces w tle, który w pętli uruchamiany jest co 3 sek., wygląda tak:
try {
try {
SaveLog(txt2string("Będzie zaraz fopen pliku ").append(nazwa));
struct stat stats;
struct tm dt;
char ctime_txt[70], mtime_txt[70], atime_txt[70];
stat(nazwa.c_str(), &stats);
string faccess;
dt = *(localtime(&stats.st_ctime));
sprintf(ctime_txt, "created: %4d-%02d-%02d %02d:%02d:%02d", dt.tm_year + 1900, dt.tm_mon+1, dt.tm_mday, dt.tm_hour, dt.tm_min, dt.tm_sec);
dt = *(localtime(&stats.st_mtime));
sprintf(mtime_txt, " modified: %4d-%02d-%02d %02d:%02d:%02d", dt.tm_year + 1900, dt.tm_mon+1, dt.tm_mday, dt.tm_hour, dt.tm_min, dt.tm_sec);
dt = *(localtime(&stats.st_atime));
sprintf(atime_txt, " accessed: %4d-%02d-%02d %02d:%02d:%02d", dt.tm_year + 1900, dt.tm_mon+1, dt.tm_mday, dt.tm_hour, dt.tm_min, dt.tm_sec);
faccess = "File access: ";
if (stats.st_mode & R_OK) faccess.append("r"); else faccess.append("-");
if (stats.st_mode & W_OK) faccess.append("w"); else faccess.append("-");
if (stats.st_mode & X_OK) faccess.append("x"); else faccess.append("-");
SaveLog(txt2string(ctime_txt).append(txt2string(mtime_txt)).append(txt2string(atime_txt)).append(" fileSize=").append(int2string(stats.st_size)).append("B ").append(faccess));
SaveLog("before fopen");
fi = fopen(nazwa.c_str(), "rb");
SaveLog("after fopen");
} catch (exception &e) {
SaveLog("exception przy fopen");
SaveLog(txt2string("Błąd otwierania pliku '").append(nazwa).append("': ").append(e.what()));
}
}
catch (...) {
SaveLog(txt2string("Nieudane otwieranie pliku '").append(nazwa).append("'"));
}
log = DEBUGproc_txt;
log.append(" try #0.b ");
SaveLog(log);
Do loga zapisuje mi on:
2022-11-07 17:12:45 Będzie zaraz fopen pliku in/zt.in
2022-11-07 17:12:45 created: 2022-11-07 17:00:03 modified: 2022-11-07 17:00:03 accessed: 2022-11-07 17:00:05 fileSize=717B File access: r--
2022-11-07 17:12:45 before fopen
2022-11-07 17:12:45 after fopen
2022-11-07 17:12:45 __ DEBUG_proc: main > GetInVector try #0.b
2022-11-07 17:12:48 Będzie zaraz fopen pliku in/zt.in
2022-11-07 17:12:48 created: 2022-11-07 17:00:03 modified: 2022-11-07 17:00:03 accessed: 2022-11-07 17:00:05 fileSize=717B File access: r--
2022-11-07 17:12:48 before fopen
2022-11-07 17:12:48 after fopen
2022-11-07 17:12:48 __ DEBUG_proc: main > GetInVector try #0.b
2022-11-07 17:12:51 Będzie zaraz fopen pliku in/zt.in
2022-11-07 17:12:51 created: 2022-11-07 17:00:03 modified: 2022-11-07 17:00:03 accessed: 2022-11-07 17:00:05 fileSize=717B File access: r--
2022-11-07 17:12:51 before fopen
2022-11-07 17:12:51 after fopen
2022-11-07 17:12:51 __ DEBUG_proc: main > GetInVector try #0.b
2022-11-07 17:12:54 Będzie zaraz fopen pliku in/zt.in
2022-11-07 17:12:54 created: 2022-11-07 17:00:03 modified: 2022-11-07 17:00:03 accessed: 2022-11-07 17:00:05 fileSize=717B File access: r--
2022-11-07 17:12:54 before fopen
W tym miejscu log się kończy, a proces przestaje istnieć.
Nie jestem w stanie dowiedzieć się, z jakiego powodu nie nagle nie udało się otworzyć pliku, a zamiast tego proces znika. TRY-CATCH powinno chyba przechwycić wyjątek, a tu nic.
Proszę o pomoc,
Jacek