-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRoomsLibLog.php
More file actions
219 lines (201 loc) · 4.88 KB
/
RoomsLibLog.php
File metadata and controls
219 lines (201 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
/**
* ルーム削除時の関連テーブル削除処理に関するライブラリ
*
* @author Noriko Arai <arai@nii.ac.jp>
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/
App::uses('Shell', 'Console/Command');
/**
* ルーム削除時の関連テーブル削除処理に関するライブラリ
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\Rooms\Lib
*/
class RoomsLibLog {
/**
* シェル開始時間
*
* @var float
*/
private static $__shellStartTime;
/**
* 処理開始時間
*
* @var float
*/
private static $__procStartTime;
/**
* debugとしてログ出力
*
* @param Shell|null $Shell 実行シェル
* @param string $message 出力するメッセージ
* @param int $indentLevel インデントレベル
* @return void
*/
public static function debugLog($Shell, $message, $indentLevel = 1) {
if (! empty($Shell)) {
$Shell->out(
"<debug>" . str_repeat(' ', $indentLevel) . "{$message}</debug>",
1,
Shell::VERBOSE
);
}
}
/**
* infoとしてログ出力
*
* @param Shell|null $Shell 実行シェル
* @param string $message 出力するメッセージ
* @param int $indentLevel インデントレベル
* @return void
*/
public static function infoLog($Shell, $message, $indentLevel = 1) {
if (! empty($Shell)) {
$Shell->out(
"<info>" . str_repeat(' ', $indentLevel) . "{$message}</info>",
1,
Shell::VERBOSE
);
}
}
/**
* successとしてログ出力
*
* @param Shell|null $Shell 実行シェル
* @param string $message 出力するメッセージ
* @param int $indentLevel インデントレベル
* @return void
*/
public static function successLog($Shell, $message, $indentLevel = 1) {
if (! empty($Shell)) {
$Shell->out(
"<success>" . str_repeat(' ', $indentLevel) . "{$message}</success>",
1,
Shell::VERBOSE
);
}
}
/**
* シェル開始のログ出力
*
* @param Shell|null $Shell 実行シェル
* @return void
*/
public static function shellStartLog($Shell) {
self::$__shellStartTime = microtime(true);
if (! empty($Shell)) {
$Shell->out(sprintf(
"[SHELL START %s] Memory=%s",
date('Y-m-d H:i:s'),
self::getMemoryUsage()
));
}
}
/**
* シェル終了のログ出力
*
* @param Shell|null $Shell 実行シェル
* @return void
*/
public static function shellEndLog($Shell) {
$endTime = microtime(true);
if (! empty($Shell)) {
$Shell->out(sprintf(
"[SHELL E N D %s] Time=%.4f, Memory=%s",
date('Y-m-d H:i:s'),
($endTime - self::$__shellStartTime),
self::getMemoryUsage()
));
}
}
/**
* 処理開始のログ出力
*
* @param Shell|null $Shell 実行シェル
* @param string $message 出力するメッセージ
* @param int $indentLevel インデントレベル
* @return void
*/
public static function processStartLog($Shell, $message = '', $indentLevel = 1) {
self::$__procStartTime = microtime(true);
if (! empty($Shell)) {
$Shell->out(sprintf(
str_repeat(' ', $indentLevel) . "[PROCESS START %s] %s Memory=%s",
date('Y-m-d H:i:s'),
$message,
self::getMemoryUsage()
));
}
}
/**
* 処理終了のログ出力
*
* @param Shell|null $Shell 実行シェル
* @param string $message 出力するメッセージ
* @param int $indentLevel インデントレベル
* @return void
*/
public static function processEndLog($Shell, $message = '', $indentLevel = 1) {
$endTime = microtime(true);
if (! empty($Shell)) {
$Shell->out(sprintf(
str_repeat(' ', $indentLevel) . "[PROCESS E N D %s] %s Time=%.4f, Memory=%s",
date('Y-m-d H:i:s'),
$message,
($endTime - self::$__procStartTime),
self::getMemoryUsage()
));
}
}
/**
* メモリー使用量取得 単位付きで取得
*
* @return string
* @see GetMemoryUsageComponent::execute() からコードをコピペ
*/
public static function getMemoryUsage() {
$size = memory_get_usage();
return self::__formatMemory($size);
}
/**
* getPeakMemoryUsage
*
* @return string
*/
public static function getPeakMemoryUsage() {
$size = memory_get_peak_usage();
return self::__formatMemory($size);
}
/**
* メモリ使用量のフォーマット
*
* @param int $size memory使用量
* @return string
*/
private static function __formatMemory($size) {
$byte = 1024; // バイト
$mb = pow($byte, 2); // メガバイト
//$gb = pow($byte, 3); // ギガバイト
switch(true){
//case $size >= $gb:
// $target = $gb;
// $unit = 'GB';
// break;
case $size >= $mb:
$target = $mb;
$unit = 'MB';
break;
default:
$target = $byte;
$unit = 'KB';
break;
}
$newSize = round($size / $target, 3);
$fileSize = number_format($newSize, 3, '.', ',') . $unit;
return $fileSize;
}
}