0

https://i.stack.imgur.com/MC6vX.png from https://stackoverflow.com/a/21666354/433570

It's dos based solution though, can it be done for linux based system?

I'm trying to highlight stuff in my log file.

For instance, I want to highlight the line of nginx log which has slower response time than 1 sec.

** edit **

Currently I'm using hi-lock-mode
eg, I put a mark on a line that shows slow response, then use regex & hi-lock to highlight it.

I guess this is ok solution, for now.
I am wondering if there's a better solution.
hi-lock mode with user-defined function rather than regex is what I would hope for.

I would define functions, and mapping between function-color. Then I would M-x apply [function]

def slow(line):
   if ... :
     return True
   return False

slow: yellow,  
iPhone: blue,

I think this would be useful to inspect logs..
I wonder if there's a similar functionality available out there?

Community
  • 1
  • 1
eugene
  • 39,839
  • 68
  • 255
  • 489
  • Are you asking how to print to a color terminal or how to make emacs highlight a file? – SLaks Nov 20 '15 at 03:06
  • I'd like to highlight lines/words if certain conditions match. I hope I could still perform other actions provided by a text editor (preferably emacs). I don't care the mechanism. One could insert a formatting direction (like in html) to show the hightlight, or one could do that dynamically (like in emacs font-lock-mode). – eugene Nov 20 '15 at 03:10
  • Check the documentation for your favorite editor's syntax highlighting options. – SLaks Nov 20 '15 at 03:19
  • I added a bit of description to the OP. – eugene Nov 20 '15 at 03:38

1 Answers1

0

Why don't you write your own major mode for your files?

A basic major mode with font-lock support is not hard to implement. There are plenty of documentation on this on the net. All you need is a syntax table (so that Emacs would know which characters start strings etc.) and some font-lock rules for syntax highlighting.

The easiest, though, is to start with an existing one, for example ini-mode, a small major mode for editing Windows-style ini files.

Unless your files have a specific file extension or otherwise follow a specific naming convention, you might want to add an entry to magic-mode-alist, which provides you with a way to recognize specific files based on the content rather than the file name.

If you would like to see your files colored in a terminal window when viewed using more or less, you can use e2ansi, a package that use Emacs to generate an ANSI version of syntax highlighted files.

Lindydancer
  • 25,428
  • 4
  • 49
  • 68
  • oh thanks, but font-lock doesn't seem to support a generic function for rules. Can I define a rule which says, highlight a line where response time is high? `0 223.33.164.38 [20/Nov/2015:07:50:21 +0000] "PATCH /rest_api/android_devices/40932 HTTP/1.1" 200 283 0.179 0.080 "-" "Dalvik/1.6.0 (Linux; U; Android 4.4.2; SHV-E330S Build/KOT49H)" ` response time is 0.179 here. I guess I could change the format so that it reads like.. rEsPoNsE: 0.179 – eugene Nov 20 '15 at 08:14
  • On the contrary, font-lock handles custom functions very well. If you replace the regexp with a function symbol the function is called. In that you can match whatever you want. For a more advanced example, see https://github.com/Lindydancer/lisp-extra-font-lock – Lindydancer Nov 20 '15 at 08:38
  • oh.. That's nice.. except lisp is hard to read :( It would have been perfect if I could use python to define the function – eugene Nov 20 '15 at 09:22
  • I'd recommend you to learn the basics of elisp. If Emacs is your preferred editor, you would like to be able to customize it, write functions to automate tasks etc -- its an investment that is well worth it. – Lindydancer Nov 20 '15 at 09:26