11const util = require ( './util' )
22
3- const Logger = function ( ) {
4- this . name = 'unknown'
5- this . db = null
6- this . inited = false
7- }
3+ class Logger {
4+ constructor ( ) {
5+ this . name = 'unknown'
6+ this . inited = false
7+ this . db = null
8+ this . version = 2
9+ }
810
9- Logger . prototype . init = function ( name ) {
10- if ( ! this . inited ) {
11- this . name = name
11+ init ( name ) {
12+ if ( ! this . inited ) {
13+ this . name = name
1214
13- if ( util . isBrowser ( ) ) {
14- window . indexedDB = window . indexedDB || window . mozIndexedDB || window . webkitIndexedDB || window . msIndexedDB
15- this . _sql = indexedDB . open ( 'danmaku' , 2 )
16- this . _sql . addEventListener ( 'success' , e => {
17- console . log ( '连接数据库成功' )
18- this . db = event . target . result
15+ const IndexedDB = window . indexedDB || window . mozIndexedDB || window . webkitIndexedDB || window . msIndexedDB
16+ this . DB = IndexedDB . open ( 'danmaku' , this . version )
17+ this . DB . addEventListener ( 'success' , e => {
18+ console . log ( '连接数据库' )
19+ this . db = e . target . result
1920 } )
2021
21- this . _sql . addEventListener ( 'upgradeneeded' , e => {
22- this . db = event . target . result
23- this . db . createObjectStore ( 'douyu' , {
22+ this . DB . addEventListener ( 'upgradeneeded' , e => {
23+ console . log ( '升级数据库' )
24+ this . db = e . target . result
25+ const store = this . db . createObjectStore ( 'douyu' , {
2426 keyPath : 'id' ,
25- autoIncrement : true
27+ autoIncrement : true ,
2628 } )
27- this . db . createIndex ( 'roomId ' , 'roomId ' , {
29+ store . createIndex ( 'idx_room_id ' , 'room_id ' , {
2830 unique : false
2931 } )
3032 } )
3133
32- this . _sql . addEventListener ( 'error' , e => {
34+ this . DB . addEventListener ( 'error' , e => {
3335 console . log ( '连接数据库出错 Error:' , e )
3436 } )
35- } else {
36- this . _fs = require ( 'fs' )
37- }
38- this . inited = true
39- }
40- }
41-
42- if ( ! util . isBrowser ( ) ) {
43- Logger . prototype . echo = function ( data ) {
44- this . _fs . appendFile (
45- this . name ,
46- JSON . stringify ( {
47- t : new Date ( ) . getTime ( ) ,
48- frame : data
49- } ) + '\n' ,
50- function ( err ) {
51- if ( err ) {
52- console . error ( '日志保存出错, Error:' , err )
53- }
54- } )
55- }
56-
57- Logger . prototype . all = function ( ) {
58- return new Promise ( ( resolve , reject ) => {
59- this . _fs . readFile ( this . name , 'utf8' , function ( err , str ) {
60- if ( err ) {
61- reject ( err )
62- } else {
63- resolve ( str )
64- }
65- } )
66- } )
67- }
68-
69- Logger . prototype . len = function ( ) {
70- return new Promise ( async ( resolve , reject ) => {
71- const content = await this . all ( )
72- resolve ( content . split ( '\n' ) . filter ( v => v !== '' ) . length )
73- } )
74- }
75-
76- Logger . prototype . export = function ( ) {
77- return this . _fs . readFileSync ( this . name , 'utf8' )
78- }
7937
80- Logger . prototype . clear = function ( ) {
81- try {
82- return this . _fs . writeFileSync ( this . name , '' , 'utf8' )
83- } catch ( err ) {
84- return false
38+ this . inited = true
8539 }
8640 }
87- } else {
88- Logger . prototype . echo = function ( data ) {
41+
42+ echo ( data ) {
8943 if ( this . db !== null ) {
9044 const tx = this . db . transaction ( 'douyu' , 'readwrite' )
9145 const store = tx . objectStore ( 'douyu' )
9246 store . add ( {
93- roomId : this . name ,
47+ room_id : this . name ,
9448 timestamp : new Date ( ) . getTime ( ) ,
9549 frame : data
9650 } )
9751 }
9852 }
9953
100- Logger . prototype . all = function ( ) {
54+ all ( roomId ) {
10155 if ( this . db !== null ) {
10256 const tx = this . db . transaction ( 'douyu' , 'readonly' )
10357 const store = tx . objectStore ( 'douyu' )
58+ const req = ( roomId ? store . index ( 'idx_room_id' ) . getAll ( roomId ) : store . getAll ( ) )
10459 return new Promise ( function ( resolve , reject ) {
105- const req = store . getAll ( )
10660 req . addEventListener ( 'success' , function ( e ) {
107- resolve ( req . result )
61+ resolve ( e . target . result )
10862 } )
10963 req . addEventListener ( 'error' , function ( e ) {
11064 reject ( false )
11165 } )
11266 } )
11367 }
114-
11568 }
11669
117- Logger . prototype . _index = function ( roomId ) {
70+ count ( roomId ) {
11871 if ( this . db !== null ) {
11972 const tx = this . db . transaction ( 'douyu' , 'readonly' )
12073 const store = tx . objectStore ( 'douyu' )
121- const req = store . index ( 'roomId ' ) . get ( roomId )
74+ const req = ( roomId ? store . index ( 'idx_room_id ' ) . count ( roomId ) : store . count ( ) )
12275 return new Promise ( function ( resolve , reject ) {
12376 req . addEventListener ( 'success' , function ( e ) {
12477 resolve ( req . result )
@@ -128,29 +81,11 @@ if (!util.isBrowser()) {
12881 } )
12982 } )
13083 }
131-
13284 }
13385
134- Logger . prototype . len = function ( ) {
86+ async export ( roomId ) {
13587 if ( this . db !== null ) {
136- const tx = this . db . transaction ( 'douyu' , 'readonly' )
137- const store = tx . objectStore ( 'douyu' )
138- return new Promise ( function ( resolve , reject ) {
139- const req = store . count ( )
140- req . addEventListener ( 'success' , function ( e ) {
141- resolve ( req . result )
142- } )
143- req . addEventListener ( 'error' , function ( e ) {
144- reject ( false )
145- } )
146- } )
147- }
148-
149- }
150-
151- Logger . prototype . export = async function ( ) {
152- if ( this . db !== null ) {
153- const r = await ( roomId ? this . _index ( roomId ) : this . all ( ) )
88+ const r = await this . all ( roomId )
15489 const text = r . reduce ( ( arr , row ) => {
15590 const v = {
15691 timestamp : row . timestamp ,
@@ -163,16 +98,27 @@ if (!util.isBrowser()) {
16398 util . download ( this . name , text . join ( '\n' ) )
16499 return text
165100 }
166-
167101 }
168102
169- Logger . prototype . clear = function ( ) {
103+ clear ( roomId ) {
170104 if ( this . db !== null ) {
171105 const tx = this . db . transaction ( 'douyu' , 'readwrite' )
172106 const store = tx . objectStore ( 'douyu' )
173- store . clear ( )
107+ if ( roomId ) {
108+ const index = store . index ( 'idx_room_id' )
109+ const req = index . openCursor ( IDBKeyRange . only ( roomId ) )
110+ req . addEventListener ( 'success' , function ( ) {
111+ const cursor = req . result
112+ if ( cursor ) {
113+ cursor . delete ( )
114+ cursor . continue ( )
115+ }
116+ } )
117+ } else {
118+ store . clear ( )
119+ }
174120 }
175121 }
176122}
177123
178- module . exports = new Logger ( )
124+ module . exports = util . isBrowser ( ) ? Logger : require ( './loggerNode' )
0 commit comments