@@ -11,6 +11,8 @@ import {
1111 Instance as VrchatInstance ,
1212 parseLocation ,
1313} from 'vrchat-location-parser'
14+ import { getVrchatLogDir } from './utils/getVrchatLogDir'
15+ import { getLatestLogfile , isVrchatLogFile } from './utils/getLatestLogfile'
1416
1517export * from './interfaces/VrchatLogWatcherEventData'
1618
@@ -79,16 +81,9 @@ export class VrchatLogWatcher extends EventEmitter {
7981
8082 constructor ( ) {
8183 super ( )
82- this . vrchatLogDir = path . join (
83- os . homedir ( ) ,
84- 'Appdata' ,
85- 'LocalLow' ,
86- 'VRChat' ,
87- 'VRChat' ,
88- )
89-
84+ this . vrchatLogDir = getVrchatLogDir ( )
9085 // start logging on init
91- this . currentLogFile = this . getLatestLogfile ( )
86+ this . currentLogFile = getLatestLogfile ( this . vrchatLogDir )
9287 this . watchFile ( )
9388
9489 // watch new files in the loggin folder
@@ -98,10 +93,10 @@ export class VrchatLogWatcher extends EventEmitter {
9893 return
9994 }
10095 // is vrchat logfile
101- if ( ! this . isVrchatLogFile ( filename ) ) {
96+ if ( ! isVrchatLogFile ( filename ) ) {
10297 return
10398 }
104- const newLogFile = this . getLatestLogfile ( )
99+ const newLogFile = getLatestLogfile ( this . vrchatLogDir )
105100 // is same logfile that current
106101 if ( newLogFile === this . currentLogFile ) {
107102 return
@@ -111,34 +106,6 @@ export class VrchatLogWatcher extends EventEmitter {
111106 } )
112107 }
113108
114- /**
115- * check whether the filename could be a vrchat file
116- * @param filename filename of a file
117- * @returns whether the file could be a vrchat log file.
118- */
119- private isVrchatLogFile = ( filename : string ) =>
120- filename . startsWith ( 'output_log_' )
121-
122- /**
123- * get the latest log file path from vrchat folder
124- * @returns the latest logfile path if found
125- */
126- private getLatestLogfile = ( ) => {
127- const logFiles = fs
128- . readdirSync ( this . vrchatLogDir )
129- . filter ( ( x ) => this . isVrchatLogFile ( x ) )
130- . map ( ( x ) => ( {
131- filename : x ,
132- createdAt : fs . statSync ( path . join ( this . vrchatLogDir , x ) ) . ctime . getTime ( ) ,
133- } ) )
134- . sort ( ( a , b ) => b . createdAt - a . createdAt )
135-
136- if ( logFiles . length === 0 ) {
137- return null
138- }
139- return path . join ( this . vrchatLogDir , logFiles [ 0 ] . filename )
140- }
141-
142109 /**
143110 * Watches the current file
144111 */
@@ -160,6 +127,9 @@ export class VrchatLogWatcher extends EventEmitter {
160127 this . emit ( 'raw' , data )
161128 this . parseLine ( data )
162129 } )
130+ this . tail . on ( 'error' , ( error ) => {
131+ this . tail ?. unwatch ( )
132+ } )
163133 }
164134
165135 /**
0 commit comments