Merge branch 'git-describe'
[TortoiseGit.git] / src / TortoiseProc / LogFile.cpp
blobb882a2bba58b0a8ab79fdd76b3cd748df8f60a3b
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008, 2013 - TortoiseGit
4 // Copyright (C) 2007 - TortoiseSVN
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software Foundation,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "stdafx.h"
21 #include "LogFile.h"
22 #include "PathUtils.h"
24 CLogFile::CLogFile(const CString& repo)
26 m_maxlines = CRegStdDWORD(_T("Software\\TortoiseGit\\MaxLinesInLogfile"), 4000);
27 m_sRepo = repo;
30 CLogFile::~CLogFile(void)
34 bool CLogFile::Open()
36 if (m_maxlines == 0)
37 return false;
38 CTGitPath logfile = CTGitPath(CPathUtils::GetAppDataDirectory() + _T("\\logfile.txt"));
39 return Open(logfile);
42 bool CLogFile::Open(const CTGitPath& logfile)
44 m_lines.clear();
45 m_logfile = logfile;
46 if (!logfile.Exists())
48 CPathUtils::MakeSureDirectoryPathExists(logfile.GetContainingDirectory().GetWinPath());
49 return true;
52 try
54 CString strLine;
55 CStdioFile file;
56 int retrycounter = 10;
57 // try to open the file for about two seconds - some other TSVN process might be
58 // writing to the file and we just wait a little for this to finish
59 while (!file.Open(logfile.GetWinPath(), CFile::typeText | CFile::modeRead | CFile::shareDenyWrite) && retrycounter)
61 retrycounter--;
62 Sleep(200);
64 if (retrycounter == 0)
65 return false;
67 while (file.ReadString(strLine))
69 m_lines.push_back(strLine);
71 file.Close();
73 catch (CFileException* pE)
75 TRACE("CFileException loading autolist regex file\n");
76 pE->Delete();
77 return false;
79 return true;
82 bool CLogFile::AddLine(const CString& line)
84 m_lines.push_back(line);
85 return true;
88 bool CLogFile::Close()
90 if (m_maxlines == 0)
91 return false;
93 AdjustSize();
94 try
96 CString strLine;
97 CStdioFile file;
99 int retrycounter = 10;
100 // try to open the file for about two seconds - some other TSVN process might be
101 // writing to the file and we just wait a little for this to finish
102 while (!file.Open(m_logfile.GetWinPath(), CFile::typeText | CFile::modeWrite | CFile::modeCreate) && retrycounter)
104 retrycounter--;
105 Sleep(200);
107 if (retrycounter == 0)
108 return false;
110 for (std::list<CString>::const_iterator it = m_lines.begin(); it != m_lines.end(); ++it)
112 file.WriteString(*it);
113 file.WriteString(_T("\n"));
115 file.Close();
117 catch (CFileException* pE)
119 TRACE("CFileException loading autolist regex file\n");
120 pE->Delete();
121 return false;
123 return true;
126 bool CLogFile::AddTimeLine()
128 CString sLine;
129 // first add an empty line as a separator
130 m_lines.push_back(sLine);
131 // now add the time string
132 TCHAR datebuf[4096] = {0};
133 GetDateFormat(LOCALE_USER_DEFAULT, DATE_SHORTDATE, NULL, NULL, datebuf, 4096);
134 sLine = datebuf;
135 GetTimeFormat(LOCALE_USER_DEFAULT, 0, NULL, NULL, datebuf, 4096);
136 sLine += _T(" - ");
137 sLine += datebuf;
138 sLine += _T(" - ");
139 sLine += m_sRepo;
140 m_lines.push_back(sLine);
141 return true;
144 void CLogFile::AdjustSize()
146 DWORD maxlines = m_maxlines;
148 while (m_lines.size() > maxlines)
150 m_lines.pop_front();