Echo text in color: Linux — Ubuntu

Often a time, you may need to echo text in different color on terminal. Here is the way that Linux/Ubuntu works

1. set color and echo

echo -e "\E[1;32mHello World"

The text color and format is defined by the format string “\E[1;32” and option “-e” turns the feature on.

The first number in the format string specifies the display format, example options are

0=none, 1=bold, 4=underscore, 5=blink, 7=reverse, 8=concealed

The second number in the format string specifies the color, example options are

30=black, 31=red, 32=green, 33=yellow, 34=blue, 35=magenta, 36=cyan, 37=white

2. reset back to normal text, run

tput sgr0

Worknotes: Eclipse C++ Project

1. Create C++ project

File -> New -> C++ Project

Type in project name, select “Use default location” if new empty project and you want to use the default location, or deselect “Use default location” and type in you preferred location in “Location” — this also allows to create project on existing C++ codes.

2. Add/Modify include paths

right click project, select “Properties” -> “C/C++ General” -> “Paths and Symbols”, select “GNU C++” in Languages and click “Add” to add desired include paths.

The path included shall be shown as

/usr/local/xxxx/include

Note: these only applies to all common library include paths, not the project local include path.

3. set project local include path

right click project, select “Properties” -> “C/C++ Build” -> “Settings”, select GCC C++ Complier -> includes”, click the green “+” icon in “inlcude paths (-I)” window. Click on “Workplace” button in “Add directory path” pop-up window. navigates to the right project local “include” directory, then “OK”. The project local include path shall be shown as

"${workspace_loc:/${ProjName}/include}"

Viewing Log4j Event Log on Web

Event log is a great resource for tracking run time issues on fly. However, most of the event log viewer available are GUI applications. In some situations, a web based GUI log viewer will be a better and convenient tool, especially when people start paying more and more attentions to clouding their services and products.

This note records the process of finding a good open source web based log viewer for logs generated by e.g. log4j.

1. Direct logs to Database such as MongoDb using event log database appender. In my application, I used a mongoDb appender “org.log4mongo.MongoDbAppender” and a web based MongoDb Management tool “mVierer”. This combination satisfied my basic need. However, if I need advanced analytic capabilities, customized MongoDB query logic and dedicated web GUI is still needed.

2. “Log4Web” is a tool developed by Launch Software. This web application reads log4j generated xml formatted log files (with .log4j extension) and presents on the web.

Log4Web is provided in .war format and can easily be installed in any web server containers such as Apache Tomcat. After installation, the log file reading page is

http://:/Log4Web/

to make this whole thing work, the following are needed

2.1. make the application file log appender to log in xml format (see the following exampe)

<appender name="xmlAppender-rsas" class="org.apache.log4j.RollingFileAppender">
<param name="Threshold" value="INFO" />
<param name="append" value="true"/>
<param name="File" value="/var/log/xxx/fw_rsas.log4j"/>
<param name="MaxFileSize" value="10MB"/>
<param name="MaxBackupIndex" value="10"/>
<layout class="org.apache.log4j.xml.XMLLayout">
<param name="LocationInfo" value="true"/>
</layout>
</appender>

2.2. config Log4Web log file

2.2.1 import the .war file into eclipse

File –> import
select “Web/WARfile” in templates list
and then follow the steps

2.2.2 edit the configuration file

edit the web.xml file contained in Log4Web.war (Log4Web/WebContent/WEB-INF/lib/web.xml), and add the specification of the log file directory, and repackage the .war.

2.2.3 repackage the .war

Right click “Log4Web” project, Export –> WAR file

2.3. deploy the new .war file into web server container.

Everything looks working except the web page have a limit of the log data not late than 2010. I’ll find out how to fix this later.

Deploy Web application archive (WAR) in Tomcat HTTP server

Web application archive (.war file) is a JAR file used to distribute a collection of JavaServer Pages, Java Servlets, Java classes, XML files, tag libraries, static web pages (HTML and related files) and other resources that together constitute a specific web application. With well known popular HTTP server framework such as Apache Tomcat widely available, it is faster and more efficient just develop a WAR and installed into existing HTTP server container, such as Tomcat, than develop the whole thing from scratch.

Here is a procedure for deploy a ready to run WAR in Tomcat container.

1. Copy the WAR file (e.g. xxxx.war) into the “webapps” directory of Tomcat. For Ubuntu, Tomcat 8, the path to “webapps” is /usr/share/tomcat/webapps, and Tomcat 7 the path is /var/lib/tomcat7/webapps.

2. access the web application via:

http://:8080/xxxx/

where xxxx is the path to the xxxx.war file of the web application.

3. example

mViewer — a web based MongoDB management tool is provided in .war.