Skip to content

Commit 03bed93

Browse files
committed
feat(init): make provision to init log outside .bb dir
1 parent c5874ea commit 03bed93

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

src/bb-logger.sh

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,14 @@ log::init()
8888
if [[ "${fileName:0:1}" != "/" ]]; then
8989
[[ ! -d "$BB_LOG_DIR" ]] && { mkdir -p "$BB_LOG_DIR" || return 1; }
9090
[[ ! -f "$BB_LOG_DIR/$fileName" ]] && { touch "$BB_LOG_DIR/$fileName" || return 1; }
91+
unset BB_LOG
92+
declare -gx BB_LOG="${BB_LOG_DIR}/$fileName"
9193
else
9294
local fileDir="${fileName%/*}"
9395
[[ ! -d "$fileDir" ]] && { mkdir -p "$fileDir" || return 1; }
9496
[[ ! -f "$fileName" ]] && { touch "$fileName" || return 1; }
97+
unset BB_LOG
98+
declare -gx BB_LOG="$fileName"
9599
fi
96100

97101
return 0
@@ -107,6 +111,7 @@ initLog() { log::init "${1:-"debug"}"; }
107111
log::rotate()
108112
{
109113
local filePath="${BB_LOG}"
114+
local fileDir="${filePath%/*}"
110115
local fileName="${filePath##*/}"
111116
local timestamp="$(date +%s)"
112117
local archive="${BB_LOG_ARCHIVE}"
@@ -116,12 +121,12 @@ log::rotate()
116121
# timestamp the current logfile
117122
mv "$filePath" "$filePath.$timestamp"
118123
# cull excess backups
119-
files="$(find "$BB_LOG_DIR" -name "$fileName" | wc -l)"
124+
files="$(find "$fileDir" -name "$fileName" | wc -l)"
120125
diff=$(( "$files" - "$BB_LOG_BACKUPS" ))
121126
diff=$diff++
122127
if [[ "$diff" -gt 0 ]]; then
123128
c=1
124-
for file in "$BB_LOG_DIR/$fileName"*
129+
for file in "$fileDir/$fileName"*
125130
do
126131
rm -f "$file"
127132
[[ $c -eq $diff ]] && break || $c++

0 commit comments

Comments
 (0)