Vader - a simple logger for Dart

    A few days ago, I decided to feel in what state the Dart language introduced some time ago is currently located. The easiest, in my opinion, way to try the language features is to write something simple and complete on it. Without thinking twice, I decided to write a simple logger for Dart in an object-oriented style. Under the cut-out is my impression of Dart and a brief description of what happened.

    Dart, as you know, is under active development and currently has:
    1. A virtual machine for executing code locally
    2. Javascript Dart Translator
    3. A simple development environment Dart Editor, built on the basis of Eclipse and is in the alpha stage.
    4. A specialized Chromium assembly called Dartium, which allows you to run Dart applications without translation into Javascript.

    Impressions of the language itself


    The easiest way, in my opinion, is to write to Dart to someone who at least once wrote in Java, because the syntax of Dart borrowed a lot from this language. However, there is a feeling that someone abused Java very much:
    • Strange file naming conventions. The description of the recommended rules for the design of the code says that * .dart files should be named with a lowercase letter and with underscores. As I understand it, the main reason for this decision is that not all operating systems distinguish case letters. Nevertheless, it is very strange to open a file with a name in lowercase and find the class there.
    • The location of the code in the files. In Dart, there is complete chaos in the location of the code in the files. The code can be contained both in classes and outside classes. In this case, the entry point in the program is the void main () method . Even the code in the Dart standard library is a strange mixture of procedural and object-oriented code. A single file may contain a class definition and functions located outside of classes.
    • Lack of some OOP modifiers. Dart lacks the private keyword, which in most C-like languages ​​means a private class method. Instead, names of private methods and fields should begin with an underscore, which is only a convention in some other programming languages.
    • Syntactic sugar. The credit is given to the presence of certain constructions, which can be considered nothing more than syntactic sugar. For example, if a method has a short implementation, then it can be written on the same line as the signature, separated by the arrow => . There are also keywords get \ set, denoting methods - getters and methods - setters.
    • Strange recommendations for the design of the code. Dart is basically a strongly typed language, which opens up great opportunities for quick refactoring and catching errors when writing a program. However, it is recommended that you do not specify types for local variables in the code style guide. Well, very, very strange ...

    Impressions of the development environment


    Everything is very simple here - the environment is damp and with a lot of bugs. First of all, the poor completion of auto-completion and code highlighting are annoying. For example, autocompletion refuses to show private class methods inside the class itself. Although Java support provided by Eclipse DartEditor is still a long way off, the environment nevertheless catches the most obvious errors related to incorrect library import or syntax errors in the code. It even shows some features of the standard Dart library - for example, that you cannot import client (dart: html) and server libraries (dart: io) at the same time.

    Promised Code Example


    Despite the fact that similar functionality is available in the standard library, I wrote a very simple logger that allows you to output messages with different levels of logging (Debug, Info, Alert, etc.) to various output streams. The main abstractions used in the logger are:
    • core / Severity.dart - sets the message severity level from DEBUG to EMERG.
    • core / LogEntry.dart - is a single log entry that stores a text message and the importance of the message.
    • streams / Stream.dart - message output stream, sets the way messages are displayed to the user. Each of the above output streams is inherited from this class. You can also create your own output stream.

    Currently 3 output streams are implemented:
    • streams / StdoutStream.dart - standard output stream using print ()
    • streams / WebConsoleStream.dart - output to the web developer console in the browser
    • streams / FileStream.dart - output to file

    Using the logger is very simple:
    #import("vader_server.dart");
    void main(){
        Vader vader = Vader.vader(new StdoutStream());
        vader.logWarn("You don't know the power of the dark side!");
        vader.logInfo("Luke, I am your father!");
    }
    

    To get started, just import one of the library files: vader_server.dart (for logging into a file) or vader_client.dart (for logging in a browser). This separation is due to the fact that client and server libraries cannot be imported into Dart at the same time. The logger code provided with comments can be viewed at this link .

    Also popular now: