Use CAutoGeneralHandle
[TortoiseGit.git] / src / TortoiseProc / LogFile.cpp
bloba99a5090e79953e5188702dde1f2e84fba5f8afc
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2007 - TortoiseSVN
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "StdAfx.h"
20 #include "LogFile.h"
21 #include "PathUtils.h"
23 CLogFile::CLogFile(void)
25 m_maxlines = CRegStdDWORD(_T("Software\\TortoiseGit\\MaxLinesInLogfile"), 4000);
28 CLogFile::~CLogFile(void)
32 bool CLogFile::Open()
34 CTGitPath logfile = CTGitPath(CPathUtils::GetAppDataDirectory() + _T("\\logfile.txt"));
35 return Open(logfile);
38 bool CLogFile::Open(const CTGitPath& logfile)
40 m_lines.clear();
41 m_logfile = logfile;
42 if (!logfile.Exists())
44 CPathUtils::MakeSureDirectoryPathExists(logfile.GetContainingDirectory().GetWinPath());
45 return true;
48 try
50 CString strLine;
51 CStdioFile file;
52 int retrycounter = 10;
53 // try to open the file for about two seconds - some other TSVN process might be
54 // writing to the file and we just wait a little for this to finish
55 while (!file.Open(logfile.GetWinPath(), CFile::typeText | CFile::modeRead | CFile::shareDenyWrite) && retrycounter)
57 retrycounter--;
58 Sleep(200);
60 if (retrycounter == 0)
61 return false;
63 while (file.ReadString(strLine))
65 m_lines.push_back(strLine);
67 file.Close();
69 catch (CFileException* pE)
71 TRACE("CFileException loading autolist regex file\n");
72 pE->Delete();
73 return false;
75 return true;
78 bool CLogFile::AddLine(const CString& line)
80 m_lines.push_back(line);
81 return true;
84 bool CLogFile::Close()
86 AdjustSize();
87 try
89 CString strLine;
90 CStdioFile file;
92 int retrycounter = 10;
93 // try to open the file for about two seconds - some other TSVN process might be
94 // writing to the file and we just wait a little for this to finish
95 while (!file.Open(m_logfile.GetWinPath(), CFile::typeText | CFile::modeWrite | CFile::modeCreate) && retrycounter)
97 retrycounter--;
98 Sleep(200);
100 if (retrycounter == 0)
101 return false;
103 for (std::list<CString>::const_iterator it = m_lines.begin(); it != m_lines.end(); ++it)
105 file.WriteString(*it);
106 file.WriteString(_T("\n"));
108 file.Close();
110 catch (CFileException* pE)
112 TRACE("CFileException loading autolist regex file\n");
113 pE->Delete();
114 return false;
116 return true;
119 bool CLogFile::AddTimeLine()
121 CString sLine;
122 // first add an empty line as a separator
123 m_lines.push_back(sLine);
124 // now add the time string
125 TCHAR datebuf[4096] = {0};
126 GetDateFormat(LOCALE_USER_DEFAULT, DATE_SHORTDATE, NULL, NULL, datebuf, 4096);
127 sLine = datebuf;
128 GetTimeFormat(LOCALE_USER_DEFAULT, 0, NULL, NULL, datebuf, 4096);
129 sLine += _T(" - ");
130 sLine += datebuf;
131 m_lines.push_back(sLine);
132 return true;
135 void CLogFile::AdjustSize()
137 DWORD maxlines = m_maxlines;
139 while (m_lines.size() > maxlines)
141 m_lines.pop_front();