OSDN Git Service

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