bool CXmlFile::LoadXmlDocument(wxString const& file) { wxFFile f(file, _T("rb")); if (!f.IsOpened()) { const wxChar* s = wxSysErrorMsg(); if (s && *s) m_error = s; else m_error = _("Unknown error opening the file. Make sure the file can be accessed and is a well-formed XML document."); return false; } m_pDocument = new TiXmlDocument; m_pDocument->SetCondenseWhiteSpace(false); if (!m_pDocument->LoadFile(f.fp()) && m_pDocument->ErrorId() != TiXmlBase::TIXML_ERROR_DOCUMENT_EMPTY) { const char* s = m_pDocument->ErrorDesc(); if (s && *s) { m_error.Printf(_("The XML document is not well-formed: %s"), wxString(s, wxConvLibc)); } else m_error = _("Unknown error opening the file. Make sure the file can be accessed and is a well-formed XML document."); delete m_pDocument; m_pDocument = 0; return false; } return true; }
bool LoadXmlDocument(TiXmlDocument* pXmlDocument, const wxString& file, wxString* error /*=0*/) { wxFFile f(file, _T("rb")); if (!f.IsOpened()) { if (error) { const wxChar* s = wxSysErrorMsg(); if (s && *s) *error = s; else *error = _("Unknown error opening the file. Make sure the file can be accessed and is a well-formed XML document."); } return false; } if (!pXmlDocument->LoadFile(f.fp())) { if (pXmlDocument->ErrorId() != TiXmlBase::TIXML_ERROR_DOCUMENT_EMPTY) { if (error) { const char* s = pXmlDocument->ErrorDesc(); error->Printf(_("The XML document is not well-formed: %s"), wxString(s, wxConvLibc).c_str()); } return false; } } return true; }
void PostErrorMessage(const wxChar* t) { wxString text; text << t << wxT("\nVýpis: ") << wxSysErrorMsg(wxSysErrorCode()); wxMessageBox(text, wxT("Chyba!"), wxOK | wxICON_EXCLAMATION); //MessageBox(NULL, wxSysErrorMsg(wxSysErrorCode()), t, NULL); }
// This function is used to check if a syscall failed, in that case // log an appropriate message containing the errno string. inline void syscall_check( bool check, const CPath& filePath, const wxString& what) { if (!check) { AddDebugLogLineC(logCFile, CFormat(wxT("Error when %s (%s): %s")) % what % filePath % wxSysErrorMsg()); } }
uint64 CFile::GetLength() const { MULE_VALIDATE_STATE(IsOpened(), wxT("CFile: Cannot get length of closed file.")); STAT_STRUCT buf; if (STAT_FD(m_fd, &buf) == -1) { throw CIOFailureException(wxString(wxT("Failed to retrieve length of file: ")) + wxSysErrorMsg()); } return buf.st_size; }
sint64 CFile::doWrite(const void* buffer, size_t nCount) { MULE_VALIDATE_PARAMS(buffer, wxT("CFile: Invalid buffer in write operation.")); MULE_VALIDATE_STATE(IsOpened(), wxT("CFile: Cannot write to closed file.")); sint64 result = ::write(m_fd, buffer, nCount); if (result != (sint64)nCount) { throw CIOFailureException(wxString(wxT("Error writing to file: ")) + wxSysErrorMsg()); } return result; }
sint64 CFile::doSeek(sint64 offset) const { MULE_VALIDATE_STATE(IsOpened(), wxT("Cannot seek on closed file.")); MULE_VALIDATE_PARAMS(offset >= 0, wxT("Invalid position, must be positive.")); sint64 result = SEEK_FD(m_fd, offset, SEEK_SET); if (result == offset) { return result; } else if (result == wxInvalidOffset) { throw CSeekFailureException(wxString(wxT("Seeking failed: ")) + wxSysErrorMsg()); } else { throw CSeekFailureException(wxT("Seeking returned incorrect position")); } }
bool CIOThread::DoWrite(const char* pBuffer, int len) { int fd = m_pFile->fd(); if (wxWrite(fd, pBuffer, len) == len) return true; int code = wxSysErrorCode(); const wxString error = wxSysErrorMsg(code); wxMutexLocker locker(m_mutex); delete [] m_error_description; m_error_description = new wxChar[error.Len() + 1]; wxStrcpy(m_error_description, error); return false; }
bool CLogging::InitLogFile(fz::scoped_lock& l) const { if (m_logfile_initialized) return true; m_logfile_initialized = true; m_file = engine_.GetOptions().GetOption(OPTION_LOGGING_FILE); if (m_file.empty()) return false; #ifdef __WXMSW__ m_log_fd = CreateFile(m_file.wc_str(), FILE_APPEND_DATA, FILE_SHARE_DELETE | FILE_SHARE_WRITE | FILE_SHARE_READ, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); if (m_log_fd == INVALID_HANDLE_VALUE) #else m_log_fd = open(m_file.fn_str(), O_WRONLY | O_APPEND | O_CREAT | O_CLOEXEC, 0644); if (m_log_fd == -1) #endif { l.unlock(); //Avoid recursion LogMessage(MessageType::Error, _("Could not open log file: %s"), wxSysErrorMsg()); return false; } m_prefixes[static_cast<int>(MessageType::Status)] = _("Status:"); m_prefixes[static_cast<int>(MessageType::Error)] = _("Error:"); m_prefixes[static_cast<int>(MessageType::Command)] = _("Command:"); m_prefixes[static_cast<int>(MessageType::Response)] = _("Response:"); m_prefixes[static_cast<int>(MessageType::Debug_Warning)] = _("Trace:"); m_prefixes[static_cast<int>(MessageType::Debug_Info)] = m_prefixes[static_cast<int>(MessageType::Debug_Warning)]; m_prefixes[static_cast<int>(MessageType::Debug_Verbose)] = m_prefixes[static_cast<int>(MessageType::Debug_Warning)]; m_prefixes[static_cast<int>(MessageType::Debug_Debug)] = m_prefixes[static_cast<int>(MessageType::Debug_Warning)]; m_prefixes[static_cast<int>(MessageType::RawList)] = _("Listing:"); m_pid = wxGetProcessId(); m_max_size = engine_.GetOptions().GetOptionVal(OPTION_LOGGING_FILE_SIZELIMIT); if (m_max_size < 0) m_max_size = 0; else if (m_max_size > 2000) m_max_size = 2000; m_max_size *= 1024 * 1024; return true; }
void wxLog::CallDoLogNow(wxLogLevel level, const wxString& msg, const wxLogRecordInfo& info) { if ( GetRepetitionCounting() ) { if ( msg == gs_prevLog.msg ) { gs_prevLog.numRepeated++; // nothing else to do, in particular, don't log the // repeated message return; } LogLastRepeatIfNeeded(); // reset repetition counter for a new message gs_prevLog.msg = msg; gs_prevLog.level = level; gs_prevLog.info = info; } // handle extra data which may be passed to us by wxLogXXX() wxString prefix, suffix; wxUIntPtr num = 0; if ( info.GetNumValue(wxLOG_KEY_SYS_ERROR_CODE, &num) ) { const long err = static_cast<long>(num); suffix.Printf(_(" (error %ld: %s)"), err, wxSysErrorMsg(err)); } #if wxUSE_LOG_TRACE wxString str; if ( level == wxLOG_Trace && info.GetStrValue(wxLOG_KEY_TRACE_MASK, &str) ) { prefix = "(" + str + ") "; } #endif // wxUSE_LOG_TRACE DoLogRecord(level, prefix + msg + suffix, info); }
void CLogging::InitLogFile() const { if (m_logfile_initialized) return; m_logfile_initialized = true; m_file = m_pEngine->GetOptions()->GetOption(OPTION_LOGGING_FILE); if (m_file == _T("")) return; #ifdef __WXMSW__ m_log_fd = CreateFile(m_file, FILE_APPEND_DATA, FILE_SHARE_DELETE | FILE_SHARE_WRITE | FILE_SHARE_READ, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); if (m_log_fd == INVALID_HANDLE_VALUE) #else m_log_fd = open(m_file.fn_str(), O_WRONLY | O_APPEND | O_CREAT, 0644); if (m_log_fd == -1) #endif { LogMessage(Error, _("Could not open log file: %s"), wxSysErrorMsg()); return; } m_prefixes[Status] = _("Status:"); m_prefixes[Error] = _("Error:"); m_prefixes[Command] = _("Command:"); m_prefixes[Response] = _("Response:"); m_prefixes[Debug_Warning] = _("Trace:"); m_prefixes[Debug_Info] = m_prefixes[Debug_Warning]; m_prefixes[Debug_Verbose] = m_prefixes[Debug_Warning]; m_prefixes[Debug_Debug] = m_prefixes[Debug_Warning]; m_prefixes[RawList] = _("Listing:"); m_pid = wxGetProcessId(); m_max_size = m_pEngine->GetOptions()->GetOptionVal(OPTION_LOGGING_FILE_SIZELIMIT); if (m_max_size < 0) m_max_size = 0; else if (m_max_size > 2000) m_max_size = 2000; m_max_size *= 1024 * 1024; }
sint64 CFile::doRead(void* buffer, size_t count) const { MULE_VALIDATE_PARAMS(buffer, wxT("CFile: Invalid buffer in read operation.")); MULE_VALIDATE_STATE(IsOpened(), wxT("CFile: Cannot read from closed file.")); size_t totalRead = 0; while (totalRead < count) { int current = ::read(m_fd, (char*)buffer + totalRead, count - totalRead); if (current == -1) { // Read error, nothing we can do other than abort. throw CIOFailureException(wxString(wxT("Error reading from file: ")) + wxSysErrorMsg()); } else if ((totalRead + current < count) && Eof()) { // We may fail to read the specified count in a couple // of situations: EOF and interrupts. The check for EOF // is needed to avoid inf. loops. break; } totalRead += current; } return totalRead; }
/* static */ void wxDbgHelpDLL::LogError(const wxChar *func) { ::OutputDebugString(wxString::Format(_T("dbghelp: %s() failed: %s\r\n"), func, wxSysErrorMsg(::GetLastError()))); }
EWXWEXPORT(void*,ELJSysErrorMsg)(int nErrCode) { return (void*)wxSysErrorMsg((unsigned long)nErrCode); }
uint64 CFile::GetPosition() const { MULE_VALIDATE_STATE(IsOpened(), wxT("Cannot get position in closed file.")); sint64 pos = TELL_FD(m_fd); if (pos == wxInvalidOffset) { throw CSeekFailureException(wxString(wxT("Failed to retrieve position in file: ")) + wxSysErrorMsg()); } return pos; }
// return the system error message description static inline wxString wxLogSysErrorHelper(long err) { return wxString::Format(_(" (error %ld: %s)"), err, wxSysErrorMsg(err)); }
void CLogging::LogToFile(MessageType nMessageType, const wxString& msg) const { fz::scoped_lock l(mutex_); if (!m_logfile_initialized) { if (!InitLogFile(l)) { return; } } #ifdef __WXMSW__ if (m_log_fd == INVALID_HANDLE_VALUE) return; #else if (m_log_fd == -1) return; #endif fz::datetime now = fz::datetime::now(); wxString out(wxString::Format(_T("%s %u %d %s %s") #ifdef __WXMSW__ _T("\r\n"), #else _T("\n"), #endif now.format(_T("%Y-%m-%d %H:%M:%S"), fz::datetime::local), m_pid, engine_.GetEngineId(), m_prefixes[static_cast<int>(nMessageType)], msg)); const wxWX2MBbuf utf8 = out.mb_str(wxConvUTF8); if (utf8) { #ifdef __WXMSW__ if (m_max_size) { LARGE_INTEGER size; if (!GetFileSizeEx(m_log_fd, &size) || size.QuadPart > m_max_size) { CloseHandle(m_log_fd); m_log_fd = INVALID_HANDLE_VALUE; // m_log_fd might no longer be the original file. // Recheck on a new handle. Proteced with a mutex against other processes HANDLE hMutex = ::CreateMutex(0, true, _T("FileZilla 3 Logrotate Mutex")); if (!hMutex) { wxString error = wxSysErrorMsg(); l.unlock(); LogMessage(MessageType::Error, _("Could not create logging mutex: %s"), error); return; } HANDLE hFile = CreateFile(m_file.wc_str(), FILE_APPEND_DATA, FILE_SHARE_DELETE | FILE_SHARE_WRITE | FILE_SHARE_READ, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); if (hFile == INVALID_HANDLE_VALUE) { wxString error = wxSysErrorMsg(); // Oh dear.. ReleaseMutex(hMutex); CloseHandle(hMutex); l.unlock(); // Avoid recursion LogMessage(MessageType::Error, _("Could not open log file: %s"), error); return; } wxString error; if (GetFileSizeEx(hFile, &size) && size.QuadPart > m_max_size) { CloseHandle(hFile); // MoveFileEx can fail if trying to access a deleted file for which another process still has // a handle. Move it far away first. // Todo: Handle the case in which logdir and tmpdir are on different volumes. // (Why is everthing so needlessly complex on MSW?) wxString tmp = wxFileName::CreateTempFileName(_T("fz3")); MoveFileEx((m_file + _T(".1")).wc_str(), tmp.wc_str(), MOVEFILE_REPLACE_EXISTING); DeleteFile(tmp.wc_str()); MoveFileEx(m_file.wc_str(), (m_file + _T(".1")).wc_str(), MOVEFILE_REPLACE_EXISTING); m_log_fd = CreateFile(m_file.wc_str(), FILE_APPEND_DATA, FILE_SHARE_DELETE | FILE_SHARE_WRITE | FILE_SHARE_READ, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); if (m_log_fd == INVALID_HANDLE_VALUE) { // If this function would return bool, I'd return FILE_NOT_FOUND here. error = wxSysErrorMsg(); } } else m_log_fd = hFile; if (hMutex) { ReleaseMutex(hMutex); CloseHandle(hMutex); } if (!error.empty()) { l.unlock(); // Avoid recursion LogMessage(MessageType::Error, _("Could not open log file: %s"), error); return; } } } DWORD len = (DWORD)strlen((const char*)utf8); DWORD written; BOOL res = WriteFile(m_log_fd, (const char*)utf8, len, &written, 0); if (!res || written != len) { CloseHandle(m_log_fd); m_log_fd = INVALID_HANDLE_VALUE; l.unlock(); // Avoid recursion LogMessage(MessageType::Error, _("Could not write to log file: %s"), wxSysErrorMsg()); } #else if (m_max_size) { struct stat buf; int rc = fstat(m_log_fd, &buf); while (!rc && buf.st_size > m_max_size) { struct flock lock = {}; lock.l_type = F_WRLCK; lock.l_whence = SEEK_SET; lock.l_start = 0; lock.l_len = 1; int rc; // Retry through signals while ((rc = fcntl(m_log_fd, F_SETLKW, &lock)) == -1 && errno == EINTR); // Ignore any other failures int fd = open(m_file.fn_str(), O_WRONLY | O_APPEND | O_CREAT | O_CLOEXEC, 0644); if (fd == -1) { wxString error = wxSysErrorMsg(); close(m_log_fd); m_log_fd = -1; l.unlock(); // Avoid recursion LogMessage(MessageType::Error, error); return; } struct stat buf2; rc = fstat(fd, &buf2); // Different files if (!rc && buf.st_ino != buf2.st_ino) { close(m_log_fd); // Releases the lock m_log_fd = fd; buf = buf2; continue; } // The file is indeed the log file and we are holding a lock on it. // Rename it rc = rename(m_file.fn_str(), (m_file + _T(".1")).fn_str()); close(m_log_fd); close(fd); // Get the new file m_log_fd = open(m_file.fn_str(), O_WRONLY | O_APPEND | O_CREAT | O_CLOEXEC, 0644); if (m_log_fd == -1) { l.unlock(); // Avoid recursion LogMessage(MessageType::Error, wxSysErrorMsg()); return; } if (!rc) // Rename didn't fail rc = fstat(m_log_fd, &buf); } } size_t len = strlen((const char*)utf8); size_t written = write(m_log_fd, (const char*)utf8, len); if (written != len) { close(m_log_fd); m_log_fd = -1; l.unlock(); // Avoid recursion LogMessage(MessageType::Error, _("Could not write to log file: %s"), wxSysErrorMsg()); } #endif } }
BBString * bmx_wxsyserrormsg(unsigned long code) { return bbStringFromWxString(wxString(wxSysErrorMsg(code))); }
void PostErrorMessageC(wxString t) { wxString text; text << t << wxT("\nVýpis: ") << wxSysErrorMsg(wxSysErrorCode()); wxMessageBox(text, wxT("Chyba!"), wxOK | wxICON_EXCLAMATION); }