Provide (experimental) clang-format file
[TortoiseGit.git] / src / TortoiseProc / LogFile.cpp
blobccd622a8b1fb368e3e9c29f3ab75227083bc33b4
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008, 2013-2014, 2016 - 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)
25 : m_maxlines(CRegStdDWORD(L"Software\\TortoiseGit\\MaxLinesInLogfile", 4000))
26 , m_sRepo(repo)
30 CLogFile::~CLogFile(void)
34 bool CLogFile::Open()
36 if (m_maxlines == 0)
37 return false;
38 CTGitPath logfile = CTGitPath(CPathUtils::GetLocalAppDataDirectory() + L"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))
68 m_lines.push_back(strLine);
69 file.Close();
71 catch (CFileException* pE)
73 TRACE("CFileException loading autolist regex file\n");
74 pE->Delete();
75 return false;
77 return true;
80 bool CLogFile::AddLine(const CString& line)
82 m_lines.push_back(line);
83 return true;
86 bool CLogFile::Close()
88 if (m_maxlines == 0)
89 return false;
91 AdjustSize();
92 try
94 CStdioFile file;
96 int retrycounter = 10;
97 // try to open the file for about two seconds - some other TSVN process might be
98 // writing to the file and we just wait a little for this to finish
99 while (!file.Open(m_logfile.GetWinPath(), CFile::typeText | CFile::modeWrite | CFile::modeCreate) && retrycounter)
101 retrycounter--;
102 Sleep(200);
104 if (retrycounter == 0)
105 return false;
107 for (const auto& line : m_lines)
109 file.WriteString(line);
110 file.WriteString(L"\n");
112 file.Close();
114 catch (CFileException* pE)
116 TRACE("CFileException loading autolist regex file\n");
117 pE->Delete();
118 return false;
120 return true;
123 bool CLogFile::AddTimeLine()
125 CString sLine;
126 // first add an empty line as a separator
127 m_lines.push_back(sLine);
128 // now add the time string
129 TCHAR datebuf[4096] = {0};
130 GetDateFormat(LOCALE_USER_DEFAULT, DATE_SHORTDATE, nullptr, nullptr, datebuf, 4096);
131 sLine = datebuf;
132 GetTimeFormat(LOCALE_USER_DEFAULT, 0, nullptr, nullptr, datebuf, 4096);
133 sLine += L" - ";
134 sLine += datebuf;
135 sLine += L" - ";
136 sLine += m_sRepo;
137 m_lines.push_back(sLine);
138 return true;
141 void CLogFile::AdjustSize()
143 DWORD maxlines = m_maxlines;
145 while (m_lines.size() > maxlines)
146 m_lines.pop_front();