-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlogcat.el
2038 lines (1839 loc) · 75.4 KB
/
logcat.el
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; logcat.el --- Major mode for reading Android logs --- -*- lexical-binding: t -*-
;; Copyright (C) 2015 Daniel Colascione <[email protected]>
;; Author: Daniel Colascione <[email protected]>
;; Version 1
;; Date: 2015-03-18
;; Keywords: android, logfile, logcat
;; URL: https://github.com/dcolascione/logcat-mode
;; This file is not part of GNU Emacs.
;; This program is free software; you can redistribute it and/or modify it under
;; the terms of the GNU General Public License as published by the Free Software
;; Foundation; either version 3, or (at your option) any later version.
;; This program is distributed in the hope that it will be useful, but WITHOUT
;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
;; FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
;; details.
;; You should have received a copy of the GNU General Public License along with
;; GNU Emacs; see the file COPYING. If not, write to the Free Software
;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
;;; Commentary
;; logcat-mode provides a convenient interface for viewing Android
;; system log files. It supports recording, filtering, and searching
;; log output.
;;
;; It relies on fb-adb [1], which you can find at
;; https://github.com/facebook/fb-adb.
;; N.B. This package is best used when byte-compiled.
;; [1] We need fb-adb because need a binary-clean channel to the
;; device in order for logcat -B to work. The stock adb package
;; mangles LF to CRLF (because it runs all subprocesses in a pty) and
;; corrupts the logcat binary format, preventing our parsing it.
(require 'cl-lib)
(require 'bytecomp)
(require 'ring)
(require 'widget)
(require 'wid-edit)
(cl-declaim (optimize (safety 0) (speed 3)))
(defconst logcat-display-style-alist
'((threadtime . logcat-insert-record-threadtime)
(brief . logcat-insert-record-brief))
"Available log rendering styles.
Alist mapping symbol to logcat inserter function. Each function
accepts a single argument, RECORD, and inserts that record
somehow at point. The insertion function should use
`logcat-insert-as' to mark which characters in the inserted text
correspond to which aspects of a log.")
(defconst logcat--fields
'(date time pid tid priority tag message)
"List of all available fields.")
(defconst logcat--field-to-invisible-spec
(cl-loop
for field in logcat--fields
collect (cons field (intern (format "logcat-field-%s" field))))
"Alist mapping field symbols to invisibility-spec entries.")
(defun logcat--field-to-ispec (field)
(or (cdr (assq field logcat--field-to-invisible-spec))
(error "unknown field %s" field)))
(defconst logcat--filterable-fields
'(pid tid priority tag message)
"List of available filterable fields.")
(defun logcat--field-displayed-p (field)
(not (memq (logcat--field-to-ispec field)
buffer-invisibility-spec)))
(defgroup logcat nil "Customizations for `logcat-mode`"
:prefix "logcat-"
:group 'tools)
(define-widget 'logcat-schoice 'choice
"Choose among `logcat-sgroup' widgets.
Each `logcat-sgroup' widget contains a `logcat-schoice-item'
button that allows the user to check which sgroup is selected."
:format "%v")
(defun logcat--sgroup-menu-tag-get (widget)
(let ((args (widget-get widget :args))
(found nil))
(while (and args (not found))
(let ((arg (car args)))
(setf args (cdr args))
(when (eq (car-safe arg) 'logcat-schoice-item)
(setf found (widget-get arg :tag)))))
found))
(define-widget 'logcat-sgroup 'group
"One choice for a `logcat-schoice'."
:action 'widget-parent-action
:menu-tag-get 'logcat--sgroup-menu-tag-get
:format "%v")
(defun logcat-schoice-item-create (widget)
(funcall (widget-get '(choice-item) :create) widget)
(unless (widget-get widget :tag)
(let* ((parent (widget-get widget :parent))
(parent-tag (and parent (widget-get parent :tag))))
(widget-put widget :tag parent-tag))))
(define-widget 'logcat-schoice-item 'choice-item
"Widget that lets users choose among `logcat-sgroup's."
:button-face 'link
:format "%[%t%] ")
(defun logcat--transpose-infix-prefix (_widget value)
(or (ignore-errors (append (list (cadr value) (car value))
(cddr value)))
value))
(defun logcat--string-subtype-match (_widget value field)
(ignore-errors
(and (memq (nth 0 value) '(= =~))
(eq (nth 1 value) field)
(stringp (nth 2 value))
(null (nthcdr 3 value)))))
(defun logcat--make-string-subtype (field tag)
`(logcat-sgroup
:value (=~ ,field "")
:match ,(lambda (widget value)
(logcat--string-subtype-match widget value field))
:value-to-internal logcat--transpose-infix-prefix
:value-to-external logcat--transpose-infix-prefix
(logcat-schoice-item :tag ,tag :value ,field)
(logcat-schoice
:inline t
(logcat-sgroup
:inline t
(logcat-schoice-item :tag "matches" :value =~)
(regexp :format "%v"))
(logcat-sgroup
:inline t
(logcat-schoice-item :tag "equals" :value =)
(string :format "%v")))))
(defun logcat--make-numeric-subtype (field tag)
`(logcat-sgroup
:match ,(lambda (_widget value)
(ignore-errors
(and (eq (nth 0 value) '=)
(eq (nth 1 value) field)
(integerp (nth 2 value))
(null (nthcdr 3 value)))))
:value-to-internal logcat--transpose-infix-prefix
:value-to-external logcat--transpose-infix-prefix
(logcat-schoice-item :tag ,tag :value ,field)
(const :format " equals " :value =)
(integer :format "%v")))
(defun logcat--make-priority-subtype (field tag)
`(logcat-sgroup
:match ,(lambda (_widget value)
(ignore-errors
(and (memq (nth 0 value) '(< <= = >= >))
(eq (nth 1 value) field)
(memq (nth 2 value) '(E W I D V))
(null (nthcdr 3 value)))))
:value-to-internal logcat--transpose-infix-prefix
:value-to-external logcat--transpose-infix-prefix
(logcat-schoice-item :tag ,tag :value ,field)
(logcat-schoice
(logcat-schoice-item :tag "is as or more severe than" :value >=)
(logcat-schoice-item :tag "is as or less severe than" :value <=)
(logcat-schoice-item :tag "is more severe than" :value >)
(logcat-schoice-item :tag "is less severe than" :value <)
(logcat-schoice-item :tag "equals" :value =))
(logcat-schoice
(logcat-schoice-item
:tag "error" :format "%[%t%]\n" :value E
:button-face (logcat-error link))
(logcat-schoice-item
:tag "warning" :format "%[%t%]%n" :value W
:button-face (logcat-warning link))
(logcat-schoice-item
:tag "info" :format "%[%t%]%n" :value I
:button-face (logcat-info link))
(logcat-schoice-item
:tag "debug" :format "%[%t%]%n" :value D
:button-face (logcat-debug link))
(logcat-schoice-item
:tag "verbose" :format "%[%t%]%n" :value V
:button-face (logcat-verbose link)))))
(defun logcat--make-recursive-subtype (op tag)
;; N.B. We work around a widgets library bug below. We're in a
;; group, and group tries to add the correct indentation for each
;; item if that item starts on its own line. If one such item is an
;; editable-list, however, we get double indentation, because
;; editable-list also tries to add indentation. By putting the
;; newline at the start of the item format instead of at the end of
;; the previous item's format, we defeat the first check, rely
;; entirely on the editable-list formatting, and get correct
;; indentation. We should still be correct even after someone fixes
;; the widget library bug.
`(logcat-sgroup
:offset 0
:extra-offset 2
(logcat-schoice-item :tag ,tag :format "%[%t%]:" :value ,op)
(,(if (eq op 'not) 'group 'editable-list)
:inline t
:format ,(concat "\n%v" (if (eq op 'not) "" "%i\n"))
:offset 0
:extra-offset 0
:args ((logcat-filter-condition)))))
(defun logcat--make-predicate-subtype (tag)
`(logcat-sgroup
(logcat-schoice-item :tag ,tag
:value funcall
:format "%[%t%] returns true ")
(function :format "%v")))
(define-widget 'logcat-filter-condition 'lazy
"A criterion for `logcat-mode' filtering."
:format "%v"
:type `(logcat-schoice
:value (>= priority I)
,(logcat--make-string-subtype 'tag "tag")
,(logcat--make-string-subtype 'message "message")
,(logcat--make-numeric-subtype 'pid "process ID")
,(logcat--make-numeric-subtype 'tid "thread ID")
,(logcat--make-priority-subtype 'priority "priority")
,(logcat--make-predicate-subtype "a predicate")
,(logcat--make-recursive-subtype
'and "all of the following conditions hold")
,(logcat--make-recursive-subtype
'or "any of the following conditions holds")
,(logcat--make-recursive-subtype
'not "the following condition does not hold")))
(define-widget 'logcat-filter-widget 'group
"Widget for editing a single logcat filter."
:tag "test group"
:format "%v"
:args '((logcat-schoice
(logcat-schoice-item :tag "Require that" :value !)
(logcat-schoice-item :tag "Include when" :value +)
(logcat-schoice-item :tag "Exclude when" :value -))
(logcat-filter-condition)))
(define-widget 'logcat-filter-list 'editable-list
"Widget for editing a list of logcat filters."
:tag "Filters"
:offset 0
:entry-format "%i %d %v"
:args '(logcat-filter-widget))
(defcustom logcat-fb-adb-program "fb-adb"
"Location of the fb-adb executable.")
(defcustom logcat-fb-adb-arguments ""
"Extra arguments (as a shell string) to pass to fb-adb.")
(defcustom logcat-follow-link-function 'logcat-follow-link-using-locate
"Function used for following links.
It should take three arguments: the name of the file to find and
the line number (as an integer) in that file, and a string
containing fullname (package and function) information. The line
number and fullname information may be `nil'."
:type 'function)
(defcustom logcat-suggest-regular-expression t
"Suggest regular expression match for message filter.
When this variable is non-nil, `logcat-filter-tag' and `logcat-filter-message',
generate a regular expression match instead of a string literal
match in the suggested filter rule."
:type 'boolean)
(defcustom logcat-default-filters nil
"Default filters for logcat buffers."
:type '(logcat-filter-list
:format "Default filters:\n%v%i\n"))
(defcustom logcat-display-style 'threadtime
"Style in which to render log entries."
:type `(radio
,@(cl-loop for (tag . function) in logcat-display-style-alist
collect `(const ,tag))))
(defcustom logcat-default-fields logcat--fields
"Fields to display by default in logcat buffers."
:type `(set
:greedy t
,@(cl-loop for field in logcat--fields
collect `(const ,field))))
(defvar logcat-read-filter-expression-history nil
"History for `logcat-read-filter-expression'.")
(defvar-local logcat--handle-log-record-function nil
"Function used for rendering log entries.
Do not set directly: use `logcat-set-display-style'.")
(defvar-local logcat--logcat-process nil
"The logcat process object associated with the buffer.")
(defvar-local logcat--insert-record-function nil
"Function to use to insert record in the buffer.")
(defvar-local logcat--filter-function nil
"Current automatically-generated logcat filter function.")
(defvar-local logcat-filters nil
"Filters that apply to logcat messages.")
(defvar logcat--used-fields)
(defvar-local logcat--filter-ring nil
"Holds a history of filter lists.")
(defcustom logcat-filter-ring-size 64
"Number of filter sets to keep in filter history."
:type 'integer)
(defconst logcat--edit-filters-buffer-name "*logcat-filter-edit*")
(defconst logcat--java-exception-regexp
(rx bol
(+ whitespace)
"at "
(group-n 5 (+ (in "a-zA-Z0-9_.<>$")))
"("
(group-n 1
(group-n 2 (+ (not (in ": \n\t\r\v"))))
":"
(group-n 3 (+ (in "0-9"))))
")" eol)
"Regular expression matching a Java file reference.
Match group 1 is the whole link; match group 2 is the filename;
match group 3 (optional) is the line number. Match group
5 (optional) is the fullname and function name.")
(defconst logcat-priority-letters
[?? ?Q ?V ?D ?I ?W ?E ?F ?S]
"Array mapping logcat priority levels to single-letter names.")
(defconst logcat-edit-filters-intro-text
"This buffer contains a list of rules logcat-mode uses to \
filter log messages. If more than one rule matches a log message, \
the last one to match wins.")
(defface logcat-error
'((t :inherit error))
"Face used to highlight logcat error-level messages."
:group 'logcat)
(defface logcat-warning
'((t :inherit warning))
"Face used to highlight logcat warning-level messages."
:group 'logcat)
(defface logcat-info
'((t :inherit success))
"Face used to highlight logcat information-level messages."
:group 'logcat)
(defface logcat-debug
'((t :inherit default))
"Face used to highlight logcat debug-level messages."
:group 'logcat)
(defface logcat-verbose
'((t :inherit default))
"Face used to highlight logcat verbose-level messages."
:group 'logcat)
(defvar logcat-choose-file-history nil
"History of names chosen when following links.")
;; 1 date
;; 2 pid
;; 3 tid
;; 4 level
;; 5 facility
;; 6 message
(eval-and-compile
(defconst logcat-date-rx
'(: (= 2 (in "0-9")) "-" (= 2 (in "0-9"))
" "
(= 2 (in "0-9")) ":"
(= 2 (in "0-9")) ":"
(= 2 (in "0-9")) "." (= 3 (in "0-9"))))
(defconst logcat-brief-format
(rx bol
(group-n 4 (in "VDIWEFS")) ; level
"/"
(group-n 5 (*? any)) ; facility
(0+ " ") "(" (0+ " ")
(group-n 2 (1+ (in "0-9")))
"): "
(group-n 6 (0+ any))
eol))
(defconst logcat-process-format
(rx bol
(group-n 4 (in "VDIWEFS")) ; level
"(" (0+ " ")
(group-n 2 (1+ (in "0-9"))) ; pid
") "
(group-n 6 (0+ any))
eol))
(defconst logcat-tag-format
(rx bol
(group-n 4 (in "VDIWEFS")) ; level
"/"
(group-n 5 (*? any)) ; facility
(0+ " ") ":" (1+ " ")
(group-n 6 (0+ any))
eol))
(defconst logcat-thread-format
(rx bol
(group-n 4 (in "VDIWEFS")) ; level
"(" (0+ " ")
(group-n 2 (1+ (in "0-9"))) ; pid
":" (0+ " ")
(group-n 3 (1+ (in "0-9"))) ; pid
") "
(group-n 6 (0+ any))
eol))
(defconst logcat-time-format
(rx bol
(group-n 1 (eval logcat-date-rx))
(1+ " ")
(group-n 4 (in "VDIWEFS")) ; level
"/"
(group-n 5 (*? any)) ; facility
(0+ " ") "(" (0+ " ")
(group-n 2 (1+ (in "0-9")))
"): "
(group-n 6 (0+ any))
eol))
(defconst logcat-threadtime-format
(rx bol
(group-n 1 (eval logcat-date-rx))
(1+ " ")
(group-n 2 (1+ (in "0-9"))) ; pid
(1+ " ")
(group-n 3 (1+ (in "0-9"))) ; tid
(1+ " ")
(group-n 4 (in "VDIWEFS")) ; level
(1+ " ")
(group-n 5 (*? any)) ; facility
(0+ " ") ":" (1+ " ")
(group-n 6 (0+ any))
eol)))
(defconst logcat-formats
'(logcat-brief-format
logcat-process-format
logcat-tag-format
logcat-thread-format
logcat-time-format
logcat-threadtime-format))
(defconst logcat-line-re
(concat "\\(?:"
(mapconcat #'symbol-value
logcat-formats
"\\|")
"\\)"))
(defconst logcat-highlights
'((1 font-lock-preprocessor-face nil t)
(2 font-lock-string-face nil t)
(3 font-lock-string-face nil t)
(4 font-lock-keyword-face nil t)
(5 font-lock-function-name-face nil t)))
(defmacro logcat--trace (fmt &rest args)
(when nil
`(with-current-buffer (get-buffer-create "*logcat-debug*")
(goto-char (point-max))
(insert (format ,(concat fmt "\n") ,@args)))))
(defun logcat-font-lock-matcher (limit)
(while (re-search-forward logcat-line-re limit t)
(mapc #'font-lock-apply-highlight logcat-highlights)
(let ((level (and (match-beginning 4)
(save-excursion
(goto-char (match-beginning 4))
(char-after)))))
(cond ((eq level ?E)
(font-lock-apply-highlight '(6 'logcat-error)))
((eq level ?W)
(font-lock-apply-highlight '(6 'logcat-warning)))
((eq level ?I)
(font-lock-apply-highlight '(6 'logcat-info)))
((eq level ?D)
(font-lock-apply-highlight '(6 'logcat-debug)))
((eq level ?V)
(font-lock-apply-highlight '(6 'logcat-verbose)))))))
(defconst logcat-font-lock-keywords
'((logcat-font-lock-matcher)))
(defconst logcat-font-lock-defaults
'(logcat-font-lock-keywords t))
(defun logcat-record-at-point ()
"Return the logcat record at point."
(get-text-property (point) 'logcat-record))
(defun logcat-read-filter-expression (&optional initial-expression)
(let ((expression
(read-from-minibuffer
"Filter: "
(if initial-expression
(with-output-to-string (prin1 initial-expression))
"")
nil t 'logcat-read-filter-expression-history)))
;; Validate expression by side effect
(logcat--compile-filter expression)
expression))
(defun logcat--call-with-preserved-apparent-positions (function)
"Call FUNCTION and try not to be visually jarring.
Call FUNCTION while preserve the relationship between the point
and the window start for each window displaying the
current buffer.
If, however, the end of the buffer would be visible after calling
FUNCTION, make sure the end of the buffer is at the end of the
window, avoiding wasting of space."
(let ((saved
(cl-loop for window in (get-buffer-window-list nil nil t)
collect (with-selected-window window
(let* ((column (current-column))
(lines-from-top
(save-excursion
(save-restriction
(narrow-to-region (window-start) (point))
(goto-char (point-min))
(vertical-motion (point-max))))))
(logcat--trace "w:%S lt:%S" window lines-from-top)
(list window lines-from-top column))))))
(unwind-protect
(funcall function)
(cl-loop for (window lines-from-top column) in saved
do (when (window-live-p window)
(with-selected-window window
(save-excursion
(logcat--trace "Before vertical motion: at %S"
(buffer-substring-no-properties (point-at-bol) (point-at-eol)))
(vertical-motion (- lines-from-top))
(logcat--trace "After vertical motion: at %S"
(buffer-substring-no-properties (point-at-bol) (point-at-eol)))
(set-window-start (selected-window) (point))
(when (pos-visible-in-window-p (point-max))
(goto-char (point-max))
(recenter -1)))
(move-to-column column)))))))
(defun logcat--handle-filter-command (field show-message &optional do-remove)
(let* ((arg (if do-remove '- current-prefix-arg))
(base-record (or (logcat-record-at-point)
(error "no logcat record")))
(base-value (cl-ecase field
(pid (logcat-record-pid base-record))
(tid (logcat-record-tid base-record))
(priority
(intern
(format "%c"
(aref logcat-priority-letters
(logcat-record-priority base-record)))))
(tag (logcat-record-tag base-record))
(message (logcat-record-message base-record))))
(subtract (or (eq arg '-)
(and (consp arg)
(integerp (car arg))
(> (car arg) 4))))
(edit (not (memq arg '(nil -))))
(op (cond
;; DWIM when the user talks about priority.
((eq field 'priority) (if subtract '<= '>=))
((and logcat-suggest-regular-expression
(memq field '(message tag)))
'=~)
(t '=)))
(base-value (if (eq op '=~)
(concat "^" (regexp-quote base-value) "$")
base-value))
(condition `(,op ,field ,base-value))
(expr `(,(if subtract '- '!) ,condition))
(expr (if edit (logcat-read-filter-expression expr) expr)))
(logcat--call-with-preserved-apparent-positions
(lambda ()
(logcat--update-filters (append logcat-filters (list expr)))))
(when show-message
(message "Added filter: %S" expr))))
(defun logcat-filter-pid ()
"Add a PID filter to the end of the filter set.
By default, add a filter that shows only log messages from the
process that wrote the log entry under point. With M-- prefix
argument, hide all messages from this process. With C-u prefix
argument, read a filter expression that defaults to showing only
messages from this process. With C-u C-u prefix, read a filter
expression that defaults to hiding messages from this process."
(declare (interactive-only logcat-add-filter))
(interactive)
(logcat--handle-filter-command
'pid
(called-interactively-p 'interactive)))
(defun logcat-filter-tid ()
"Add a TID filter to the end of the filter set.
By default, add a filter that shows only log messages from the
thread that wrote the log entry under point. With M-- prefix
argument, hide all messages from this thread. With C-u prefix
argument, read a filter expression that defaults to showing only
messages from this thread. With C-u C-u prefix, read a filter
expression that defaults to hiding messages from this thread."
(declare (interactive-only logcat-add-filter))
(interactive)
(logcat--handle-filter-command
'tid
(called-interactively-p 'interactive)))
(defun logcat-filter-priority ()
"Add a priority filter to the end of the filter set.
By default, add a filter that shows only log messages with a
priority equal to or greater than the priority of the message
under point. With M-- prefix argument, hide all messages with a
priority less than or equal to the priority of the message under
point. With C-u prefix argument, read a filter expression that
defaults to showing only messages with priority equal to or
greater than the priority of the message under point. With C-u
C-u prefix, read a filter expression that defaults to hiding
messages with priority less than or equal to message
under point."
(declare (interactive-only logcat-add-filter))
(interactive)
(logcat--handle-filter-command
'priority
(called-interactively-p 'interactive)))
(defun logcat-filter-tag ()
"Add a tag filter to the end of the filter set.
By default, add a filter that shows only log messages with a tag
equal to that of the message under point. With M-- prefix
argument, hide all message with a tag equal to that of the
message under point. With C-u prefix argument, read a filter
expression that defaults to showing only messages with a tag
equal to that of the message under point. With C-u C-u prefix,
read a filter expression that defaults to hiding messages with
tag equal to that of the message under point.
If `logcat-suggest-regular-expression' is non-`nil', default to
using a regular expression match. Otherwise, default to a
string match."
(declare (interactive-only logcat-add-filter))
(interactive)
(logcat--handle-filter-command
'tag
(called-interactively-p 'interactive)))
(defun logcat-filter-message ()
"Add a message filter to the end of the filter set.
By default, add a filter that shows only log messages with a
message equal to that of the message under point. With M--
prefix argument, hide all message with a message equal to that of
the message under point. With C-u prefix argument, read a filter
expression that defaults to showing only messages with a message
equal to that of the message under point. With C-u C-u prefix,
read a filter expression that defaults to hiding messages with
message equal to that of the message under point.
If `logcat-suggest-regular-expression' is non-`nil', default to
using a regular expression match. Otherwise, default to a
string match."
(declare (interactive-only logcat-add-filter))
(interactive)
(logcat--handle-filter-command
'message
(called-interactively-p 'interactive)))
(defun logcat-add-filter (filter-expression)
"Add FILTER-EXPRESSION to the end of the filter set."
(interactive (list (logcat-read-filter-expression)))
(logcat--call-with-preserved-apparent-positions
(lambda ()
(logcat--update-filters (append logcat-filters
(list filter-expression)))))
(when (called-interactively-p 'interactive)
(message "Added filter specification.")))
(defun logcat-clear-filters ()
"Remove all logcat filters."
(interactive)
(logcat--call-with-preserved-apparent-positions
(lambda ()
(logcat--update-filters nil)))
(when (called-interactively-p 'interactive)
(message "Cleared filters")))
(defvar logcat-mode-filter-map
(let ((keymap (make-sparse-keymap "Filters")))
(define-key keymap [?P] '(menu-item "pid" logcat-filter-pid))
(define-key keymap [?T] '(menu-item "tid" logcat-filter-tid))
(define-key keymap [?p] '(menu-item "prio" logcat-filter-priority))
(define-key keymap [?t] '(menu-item "tag" logcat-filter-tag))
(define-key keymap [?m] '(menu-item "message" logcat-filter-message))
(define-key keymap [?e] '(menu-item "edit" logcat-edit-filters))
(define-key keymap [?a] '(menu-item "add" logcat-add-filter))
(define-key keymap [(control c)] '(menu-item "clear" logcat-clear-filters))
keymap)
"Keymap for logcat-mode filters.")
(defun logcat--handle-display-command (field enable show-message)
(let* ((ispec (logcat--field-to-ispec field))
(make-visible (cond ((null enable)
(memq ispec buffer-invisibility-spec))
((< enable 0) nil)
(t t))))
(if make-visible
(remove-from-invisibility-spec ispec)
(add-to-invisibility-spec ispec))
(redraw-display)
(when show-message
(message "%s %s" (if make-visible "Displaying" "Hiding") field))))
(defun logcat-display-date (&optional enable)
"Show or hide the date portion of logcat messages.
If ENABLE is greater than or equal to zero, show the date.
If ENABLE is less than zero, hide it. If ENABLE is nil, toggle."
(interactive)
(logcat--handle-display-command
'date
enable
(called-interactively-p 'interactive)))
(defun logcat-display-time (&optional enable)
"Show or hide the time portion of logcat messages.
If ENABLE is greater than or equal to zero, show the time.
If ENABLE is less than zero, hide it. If ENABLE is nil, toggle."
(interactive)
(logcat--handle-display-command
'time
enable
(called-interactively-p 'interactive)))
(defun logcat-display-pid (&optional enable)
"Show or hide the PID portion of logcat messages.
If ENABLE is greater than or equal to zero, show the PID.
If ENABLE is less than zero, hide it. If ENABLE is nil, toggle."
(interactive)
(logcat--handle-display-command
'pid
enable
(called-interactively-p 'interactive)))
(defun logcat-display-tid (&optional enable)
"Show or hide the TID portion of logcat messages.
If ENABLE is greater than or equal to zero, show the TID.
If ENABLE is less than zero, hide it. If ENABLE is nil, toggle."
(interactive)
(logcat--handle-display-command
'tid
enable
(called-interactively-p 'interactive)))
(defun logcat-display-priority (&optional enable)
"Show or hide the priority portion of logcat messages.
If ENABLE is greater than or equal to zero, show the priority.
If ENABLE is less than zero, hide it. If ENABLE is nil, toggle."
(interactive)
(logcat--handle-display-command
'priority
enable
(called-interactively-p 'interactive)))
(defun logcat-display-tag (&optional enable)
"Show or hide the tag portion of logcat messages.
If ENABLE is greater than or equal to zero, show the tag.
If ENABLE is less than zero, hide it. If ENABLE is nil, toggle."
(interactive)
(logcat--handle-display-command
'tag
enable
(called-interactively-p 'interactive)))
(defun logcat-display-message (&optional enable)
"Show or hide the message portion of logcat messages.
If ENABLE is greater than or equal to zero, show the message.
If ENABLE is less than zero, hide it. If ENABLE is nil, toggle."
(interactive)
(logcat--handle-display-command
'message
enable
(called-interactively-p 'interactive)))
(defun logcat-display-all ()
"Display all log fields."
(interactive)
(setf buffer-invisibility-spec '(logcat-filtered))
(redraw-display))
(defun logcat-set-display-style (style &optional no-reformat)
"Set the style in which `logcat-mode' will render log entries.
STYLE is a style name from `logcat-display-style-alist'.
If NO-REFORMAT is non-`nil' (or, interactively, if called with a
prefix argument), `logcat-set-display-style' does not reformat
existing entries."
(interactive (list
(intern (completing-read
"Logcat display style: "
(mapcar #'car logcat-display-style-alist)
nil
t))
current-prefix-arg))
(setf logcat--insert-record-function
(or (cdr (assq style logcat-display-style-alist))
(error "no logcat display style called %S" style)))
(unless no-reformat
(logcat--refresh-records t)))
(defvar logcat-mode-display-map
(let ((keymap (make-sparse-keymap "Display")))
(define-key keymap [?d] '(menu-item "date" logcat-display-date))
(define-key keymap [?i] '(menu-item "time" logcat-display-time))
(define-key keymap [?P] '(menu-item "pid" logcat-display-pid))
(define-key keymap [?T] '(menu-item "tid" logcat-display-tid))
(define-key keymap [?p] '(menu-item "prio" logcat-display-priority))
(define-key keymap [?t] '(menu-item "tag" logcat-display-tag))
(define-key keymap [?m] '(menu-item "message" logcat-display-message))
(define-key keymap [?a] '(menu-item "all" logcat-display-all))
(define-key keymap [(control s)]
'(menu-item "style" logcat-set-display-style))
keymap))
(defun logcat--font-lock-fontify-buffer ()
;; Use Emacs 25 functions when available.
(if (and (fboundp 'font-lock-flush)
(fboundp 'font-lock-ensure))
(progn
(funcall 'font-lock-flush)
(funcall 'font-lock-ensure))
(with-no-warnings
(font-lock-fontify-buffer))))
(defun logcat-log-info ()
(interactive)
(cond (mark-active
(let ((start (point))
(end (mark)))
(when (< end start)
(cl-psetf start end end start))
(let* ((r-start (or (save-excursion
(goto-char start)
(logcat-record-at-point))
(error "no log record at %s"
(if (eql start (point)) "point" "mark"))))
(r-end (or (save-excursion
(goto-char end)
(logcat-record-at-point))
(error "no log record at %s"
(if (eql end (point)) "point" "mark"))))
(time-diff (time-subtract
(logcat-record-timestamp r-end)
(logcat-record-timestamp r-start))))
(message "Time difference: %gms"
(* 1000 (float-time time-diff))))))
(t
(let ((record (save-excursion
(logcat--forward-invisible-character
most-positive-fixnum)
(logcat-record-at-point))))
(unless record
(error "no record at point"))
(with-temp-buffer
(logcat-mode)
(logcat-insert-record-threadtime record)
(logcat--font-lock-fontify-buffer)
(message "%s" (buffer-substring (point-min) (1- (point-max)))))))))
(defun logcat--strip-invisible-text (text)
(let ((length (length text)))
(if (and (not (invisible-p (get-text-property 0 'invisible text)))
(= (next-single-property-change 0 'invisible text length)
length))
text
(let ((pos 0)
(parts nil))
(while (< pos length)
(let ((start pos))
(setf pos (next-single-property-change
pos 'invisible text length))
(unless (invisible-p (get-text-property start 'invisible text))
(push (substring text start pos) parts))))
(cond ((cdr parts)
(apply #'concat (nreverse parts)))
((car parts))
(t ""))))))
(defmacro logcat--with-buffer-modifications (&rest body)
(declare (indent 0))
`(let ((inhibit-read-only t))
,@body))
(defun logcat-flush-filtered-records ()
"Delete log records currently hidden by filters."
(interactive)
(let ((nr-deleted 0))
(logcat--with-buffer-modifications
(save-restriction
(widen)
(let ((pos (point-min)))
(while (not (eq pos (point-max)))
(let* ((invisible (get-text-property pos 'invisible))
(start pos))
(setf pos (next-single-property-change
pos 'invisible nil (point-max)))
(when (eq invisible 'logcat-filtered)
(delete-region start pos)
(setf pos start)
(cl-incf nr-deleted)))))))
(message "Deleted %d filtered records" nr-deleted)))
(defun logcat-delete-output ()
"Delete logcat output from the buffer."
(interactive)
(save-excursion
(save-restriction)
(widen)
(logcat--with-buffer-modifications
(delete-region (point-min) (point-max)))))
(defun logcat--field-menu-label (field)
(cl-case field
(pid "process ID")
(tid "thread ID")
(t (symbol-name field))))
(defun logcat--inside-record-p (pos)
(save-restriction
(widen)
(and (not (eql pos (point-min)))
(not (eql pos (point-max)))
(eq (get-text-property (1- pos) 'logcat-record)
(get-text-property pos 'logcat-record)))))
(defun logcat--check-region (beg end)
(when (logcat--inside-record-p beg)
(error "cannot delete partial region"))
(when (logcat--inside-record-p end)
(if (and (eql (char-after end) ?\n)
(not (logcat--inside-record-p (1+ end))))
(cl-incf end)
(error "cannot delete partial region")))
(cons beg end))
(defun logcat-delete-region (beg end)
"Delete a region of the logcat buffer."
(interactive "r")
(cl-destructuring-bind (beg . end) (logcat--check-region beg end)
(logcat--with-buffer-modifications
(delete-region beg end))))
(defun logcat--buffer-substring-wrapper
(orig-fn beg end &optional delete)
(if delete
(cl-destructuring-bind (beg . end) (logcat--check-region beg end)
(logcat--with-buffer-modifications
(funcall orig-fn beg end delete)))
(funcall orig-fn beg end delete)))
(defvar logcat-mode-map
(let ((keymap (make-sparse-keymap)))
(define-key keymap [(control c) (control f)] logcat-mode-filter-map)
(define-key keymap [(control c) (control d)] logcat-mode-display-map)
(define-key keymap [(control c) (control k)]
'logcat-flush-filtered-records)
(define-key keymap [remap delete-region] 'logcat-delete-region)
(define-key keymap [remap undo] 'logcat-filter-undo)
(define-key keymap [(control ?.)] 'logcat-log-info)
(define-key keymap [(control c) (meta o)] 'logcat-delete-output)