<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>AdaCore - The GNAT Pro Company &#187; Ada / Ada 2005 / Ada 2012</title>
	<atom:link href="http://www2.adacore.com/category/developers-center/technologies/ada/feed/" rel="self" type="application/rss+xml" />
	<link>http://www2.adacore.com</link>
	<description>AdaCore technology and news</description>
	<lastBuildDate>Tue, 07 Feb 2012 12:00:00 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Gem #117: Design Pattern: Overridable Class Attributes in Ada 2012</title>
		<link>http://www2.adacore.com/2012/01/30/gem-117-design-pattern-overridable-class-attributes-in-ada-2012/</link>
		<comments>http://www2.adacore.com/2012/01/30/gem-117-design-pattern-overridable-class-attributes-in-ada-2012/#comments</comments>
		<pubDate>Mon, 30 Jan 2012 13:32:46 +0000</pubDate>
		<dc:creator>Emmanuel Briot, of AdaCore</dc:creator>
				<category><![CDATA[Ada / Ada 2005 / Ada 2012]]></category>
		<category><![CDATA[Development Log]]></category>
		<category><![CDATA[Devt log - Gem of the Week]]></category>

		<guid isPermaLink="false">http://www2.adacore.com/?p=13448</guid>
		<description><![CDATA[<p>In this Gem we consider how to realize the capability of "class attributes" (such as supported
in Python) using Ada.</p>]]></description>
			<content:encoded><![CDATA[<strong>Let&#8217;s get started&#8230;</strong>

<p>Most  object-oriented programming  languages provide  a facility for declaring
variables that are  shared by all objects of a given class. In C++, these are
called &#8220;static members&#8221; (and use  the &#8220;static&#8221; keyword), and similarly Python has the notion
of &#8220;class attributes&#8221;.</p>

<p>Let&#8217;s consider an example where this is useful. For instance, let&#8217;s say we want to
define the notion of a block of text that is generated by expanding a template
(perhaps after we replace some parameters in that template, as can be done with
AWS&#8217;s templates parser, for instance). Once we have computed those parameters,
we might want to generate multiple outputs (for instance HTML and CSV). Only
the template needs to change, not the computation of the parameters.</p>

<p>Typically, such as in Python, the template could  be implemented as a class
attribute of the Text_Block class. We can then create templates that need the
same information but have a different output simply by extending that class:</p>

<pre>
   class Text_Block(object):
       template = &quot;somefile.txt&quot;
       def render (self):
           # ... compute some parameters
           # <b>Then</b> <b>do</b> template expansion
           print &quot;processing %s&quot; % self.__class__.template

   class Html_Block(Text_Block):
       template = &quot;otherfile.html&quot;
</pre>

<p>In this example, we chose to use a class attribute rather than the usual
instance attribute (<code>self.template</code>). This example comes from the
implementation of GnatTracker: in the web server we create a new instance of
Text_Block for every request we have to serve. For this, we use a registry that
maps the URL to the class we need to create. It is thus easier to create a new
instance without specifying the template name as a parameter, which would be
required if the template name was stored in the instance. Another reason (though
not really applicable here) is to save memory, which would be important in cases
where there are thousands of instances of the class.</p>

<p>Of course, the approach proposed in this Gem is not the only way to solve the
basic problem, but it serves as a nice example of one of the new Ada 2012
features.</p>

<p>C++, like Ada, does not provide a way to override a static class member, so it
would use a similar solution as described below.</p>

<p>Since Ada has no notion of an overridable class attribute, we&#8217;ll model
it using a subprogram instead (the only way to get dispatching in Ada). The important
point here is that we want to be able to override the template name in child classes,
so we cannot use a simple constant in the package spec or body.</p>

<pre>
   <b>type</b> Text_Block <b>is</b> <b>tagged</b> <b>null</b> <b>record</b>;
   <b>function</b> Template (Self : Text_Block) <b>return</b> String;
   <b>function</b> Render (Self : Text_Block) <b>return</b> String;

   <b>function</b> Template (Self : Text_Block) <b>return</b> String <b>is</b>
      <b>pragma</b> Unreferenced (Self);
   <b>begin</b>
      <b>return</b> &quot;file_name.txt&quot;;
   <b>end</b> Template;
</pre>

<p>The parameter Self is only used for dispatching (so that children
of Text_Block can override this function). Since we prefer to compile
with &#8220;-gnatwu&#8221; to get a warning on unused entities, we indicate to the
compiler that it is expected that Self is unreferenced.</p>

<p>We could make the function Template inlinable, which might be useful
in a few cases (for instance if called from Render in a nondispatching
call), but in general there will be no benefit because Template will be
a dispatching call, which requires an indirect call and thus
wouldn&#8217;t benefit from inlining.</p>

<p>And that&#8217;s it. We have the Ada equivalent of a Python class member.</p>

<p>But so far there is nothing new here, and this approach is rather heavy to write.
For instance, the body of Render could contain code like:</p>

<pre>
   <b>pragma</b> Ada95;

   <b>function</b> Render (Self : Text_Block) <b>return</b> String <b>is</b>
      T : <b>constant</b> String := Template (Text_Block'Class (Self));
   <b>begin</b>
      ..  prepare the parameters <b>for</b> template expansion
      ..  substitute <b>in</b> the template <b>and</b> <b>return</b> it
   <b>end</b> Render;
</pre>

<p>Fortunately, Ada 2012 provides an easier way to write this, using the new
feature of <i>expression functions</i>. Since Template is a function that returns a
constant, we can declare that directly in the spec, and remove the body
altogether. The spec will thus look like:</p>

<pre>
   <b>pragma</b> Ada_2012;

   <b>type</b> Text_Block <b>is</b> <b>tagged</b> <b>null</b> <b>record</b>;
   <b>function</b> Template (Self : Text_Block) <b>return</b> String
      <b>is</b> (&quot;filename.txt&quot;);
   <b>function</b> Render (Self : Text_Block) <b>return</b> String;
</pre>

<p>This is a much lighter syntax, and much closer to how one would do
it in Python (except we use a function instead of a variable to represent
a class member). A child of Text_Block would override Template using
the same notation:</p>

<pre>
   <b>type</b> Html_Block <b>is</b> <b>new</b> Text_Block <b>with</b> <b>null</b> <b>record</b>;
   <b>overriding</b> <b>function</b> Template (Self : Text_Block) <b>return</b> String
      <b>is</b> (&quot;otherfile.html&quot;);
</pre>

<p>Compared to Python, this is in fact more powerful, because some of the children
could provide a more complex body for Template, so we are not limited to using the
value of a simple variable as in Python. In fact, we can do this in the spec itself,
by using a <i>conditional expression</i> (another new feature of Ada 2012):</p>

<pre>
   <b>pragma</b> Ada_2012;

   <b>type</b> Text_Block <b>is</b> <b>tagged</b> <b>null</b> <b>record</b>;
   <b>function</b> Template (Self : Text_Block) <b>return</b> String
       <b>is</b> (<b>if</b> Self.Blah <b>then</b> &quot;filename.html&quot; <b>else</b> &quot;file2.json&quot;);
   <b>function</b> Render (Self : Text_Block) <b>return</b> String;
</pre>

<p>Finally, we can also make the body of Render slightly more familiar (in terms of
object-oriented notation) using the dotted notation introduced in Ada 2005:</p>

<pre>
   <b>function</b> Render (Self : Text_Block) <b>return</b> String <b>is</b>
      T : <b>constant</b> String := Text_Block'Class (Self).Template;
   <b>begin</b>
      ..  prepare the parameters <b>for</b> template expansion
      ..  substitute <b>in</b> the template <b>and</b> <b>return</b> it
   <b>end</b> Render;
</pre>

<p>Now the call to Template looks closer to how it would appear in those languages
that provide overridable class members. Some will argue that this doesn&#8217;t look like
a function call and thus is less readable, since we don&#8217;t know that we are
calling a function. This is a matter of taste, but at least we have the
choice.</p>

<p>There is one thing we have lost, temporarily, in the declaration of
Template. If we compile with -gnatwu, the compiler will complain that
Self is unreferenced. There is currently no way to add a pragma Unreferenced
within an expression function. This has generated a discussion here
at AdaCore and the issue is not resolved yet. The current two proposals are
either to always omit the unused parameter warning when a function has a single
parameter and it controls dispatching (precisely to facilitate this class member
pattern), or else to use an Ada 2012 aspect for this, as in the following:</p>

<pre>
   <b>function</b> Template (Self : Text_Block) <b>return</b> String
      <b>is</b> (&quot;filename.html&quot;)
   <b>with</b> Unreferenced =&gt; Self;
</pre>

<p>Note also that the use of expression functions in this Gem requires
a very recent version of GNAT: the expression function feature
wasn&#8217;t available in older versions, and the initial implementation had
some limitations.
</p>
]]></content:encoded>
			<wfw:commentRss>http://www2.adacore.com/2012/01/30/gem-117-design-pattern-overridable-class-attributes-in-ada-2012/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Gem #116: Ada and C++ Exceptions</title>
		<link>http://www2.adacore.com/2012/01/16/gem-114-ada-and-c-exceptions/</link>
		<comments>http://www2.adacore.com/2012/01/16/gem-114-ada-and-c-exceptions/#comments</comments>
		<pubDate>Mon, 16 Jan 2012 11:42:36 +0000</pubDate>
		<dc:creator>Quentin Ochem, AdaCore</dc:creator>
				<category><![CDATA[Ada / Ada 2005 / Ada 2012]]></category>
		<category><![CDATA[Development Log]]></category>
		<category><![CDATA[Devt log - Gem of the Week]]></category>

		<guid isPermaLink="false">http://www2.adacore.com/?p=13409</guid>
		<description><![CDATA[One of the main issues raised by interfacing Ada and C++ is propagation of exceptions from one environment to the other. In this Gem, we’ll demonstrate how the new exception mechanism implemented in GNAT allows catching Ada exceptions in C++, and vice versa. Note that the following code will work starting with GNAT Pro 7.]]></description>
			<content:encoded><![CDATA[<h3>Let’s get started&#8230;</h3>

<p>So we’ve decided to have some fun and are building an application that combines
Ada and C++. So far, so good. We used the C++ to Ada binding generator to bind
the C++ classes directly to Ada, and extend them. However, there’s something
that needs to be considered carefully –- what would happen if an
exception were thrown/raised by C++ vs. Ada code? Let’s try it out:</p>

<h3>Step 1 &#8212; The C++ code</h3>

<p>We’re going to write some very simple C++ code, just two classes, one inheriting
from the other, and overriding a &#8220;compute&#8221; primitive:</p>

<pre>
<b>class</b> COperation {
  <b>public</b>:
  <b>virtual int</b> compute (<b>int</b> a, <b>int</b> b);
  COperation ();
};

<b>class</b> CDivision : <b>public</b> COperation {
  <b>public</b>:
  <b>virtual int</b> compute (<b>int</b> a, <b>int</b> b);
  CDivision ();
};

<b>int</b> CDivision::compute (<b>int</b> a, <b>int</b> b) {
  <b>if</b> (b == 0) {
    <b>throw new</b> Problem (&quot;Division by 0 in C++!&quot;);
  } <b>else</b> {
    <b>return</b> a / b;
  }
}
</pre>

<p>In the computation of CDivision, we’ll raise a C++ exception if a divide-by-zero
occurs. Let’s add a second subprogram doing a dynamic dispatch:</p>

<pre>
<b>void</b> cpp_main (COperation &amp; op) {
  <b>try</b> {
     cout &lt;&lt; op.compute (1, 0);
  } <b>catch</b> (...) {
    cout &lt;&lt; &quot;Unknown exception caught, rethrowing...&quot; &lt;&lt; &quot;\n&quot;;
    <b>throw</b>;
  }
}
</pre>

<p>This function calls the compute operation, catches the exception to display a message, and
then rethrows it.</p>

<p>We’re now going to bind this to Ada. Assuming all specifications are in a 
file base.hh, a simple call to gcc will do it:</p>

<pre>
g++ -fdump-ada-spec base.hh
</pre>

<h3>Step 2 &#8212; Extending the C++ code in Ada</h3>

<p>There is an implementation of CDivision in C++. After the binding phase, let’s 
implement the same code in Ada:</p>

<pre><b>type</b> Ada_Division <b>is</b> <b>new</b> base_hh.Class_COperation.COperation <b>with</b>
     <b>null</b> <b>record</b>;

<b>function</b> Compute
  (this : <b>access</b> Ada_Division;
   a : int;
   b : int) <b>return</b> int <b>is</b>
<b>begin</b>
   <b>if</b> b = 0 <b>then</b>
      <b>raise</b> Constraint_Error <b>with</b> &quot;Division by 0 in Ada!&quot;;
   <b>end</b> <b>if</b>;
   <b>return</b> a / b;
<b>end</b> Compute;
</pre>

<p>We now have three classes in the application. One root C++ class, one pure C++
child, and one mixed Ada/C++ child. Let’s see how things how things work
from there.</p>

<h3>Step 3 &#8212; Catching a C++ exception in Ada</h3>

<p>C++ exceptions do not have a known name once they reach the Ada world.
They act just as if they were declared in the body of a package, so the only
way to catch them is to use a general exception handler. Consider the following
code:</p>

<pre>
   T_Cpp : <b>aliased</b> base_hh.Class_CDivision.CDivision;
   X : Interfaces.C.Int;
<b>begin</b> 
   X :=  T_Cpp.compute (1, 0);
<b>exception</b>
   <b>when</b> <b>others</b> =&gt;
      Put_Line (&quot;[1] Exception caught...&quot;);
<b>end</b>;

</pre>

<p>This will print &#8220;[1] Exception caught&#8230;&#8221; on the screen.</p>

<h3>Step 4 &#8212; Handling exceptions across languages</h3>

<p>Now let’s be a little bit more ambitious. We’re going to call the cpp_main
function with an object coming from Ada:</p>

<pre>
   T_Ada : <b>aliased</b> Base_Ada.Ada_Division;
<b>begin</b>
   base_hh.cpp_main (T_Ada'<b>Access</b>);
<b>exception</b>
   <b>when</b> Constraint_Error =&gt;
      Put_Line (&quot;[2] Constraint_Error caught...&quot;);
   <b>when</b> <b>others</b> =&gt;
      Put_Line (&quot;[2] Exception caught...&quot;);
<b>end</b>;
</pre>

<p>Now, looking back at the cpp_main implementation, we call compute with (1, 0)
as the arguments. This will trigger an exception from the Ada implementation, 
which is going to be caught first by the C++ code, printing &#8220;Unknown
exception caught, rethrowing&#8230;&#8221; on the screen. The same exception is then
rethrown by the C++ side, ending up back in the Ada code above,
printing &#8220;[2] Constraint_Error caught&#8230;&#8221;.</p>

<p></p>
]]></content:encoded>
			<wfw:commentRss>http://www2.adacore.com/2012/01/16/gem-114-ada-and-c-exceptions/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Ada Connection 2011 &#8211; Assuring Software Reliability While Using Web Services and Commercial Products</title>
		<link>http://www2.adacore.com/2012/01/03/assuring-reliability-web-services/</link>
		<comments>http://www2.adacore.com/2012/01/03/assuring-reliability-web-services/#comments</comments>
		<pubDate>Tue, 03 Jan 2012 14:55:41 +0000</pubDate>
		<dc:creator>AdaCore</dc:creator>
				<category><![CDATA[Ada / Ada 2005 / Ada 2012]]></category>
		<category><![CDATA[Development Log]]></category>

		<guid isPermaLink="false">http://www2.adacore.com/?p=13639</guid>
		<description><![CDATA[Here&#8217;s this Monday&#8217;s installment from the Ada Connection 2011 talks. Jeff O’Leary from the Federal Aviation Administration gives a talk on Assuring Software Reliability While Using Web Services and Commercial Products




A new film added every Monday. To view all the films we&#8217;ve added to date, please visit the Ada Lecture Series. 
]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s this Monday&#8217;s installment from the <a href="http://conferences.ncl.ac.uk/adaconnection2011/" target="_blank">Ada Connection 2011</a> talks. Jeff O’Leary from the Federal Aviation Administration gives a talk on Assuring Software Reliability While Using Web Services and Commercial Products</p>
<br/>

<iframe width="480" height="274" src="http://www.youtube.com/embed/KJR_j_NHTm8" frameborder="0" allowfullscreen></iframe>

<p>A new film added every Monday. To view all the films we&#8217;ve added to date, please<a href="http://www.adacore.com/home/ada_answers/lectures/ada-connection-2011/"> visit the Ada Lecture Series</a>. </p>
]]></content:encoded>
			<wfw:commentRss>http://www2.adacore.com/2012/01/03/assuring-reliability-web-services/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Gem #115: Lego Mindstorms Ada Environment &#8212; Part 2</title>
		<link>http://www2.adacore.com/2011/12/05/gem-115-lego-mindstorms-ada-environment-part-2/</link>
		<comments>http://www2.adacore.com/2011/12/05/gem-115-lego-mindstorms-ada-environment-part-2/#comments</comments>
		<pubDate>Mon, 05 Dec 2011 14:47:20 +0000</pubDate>
		<dc:creator>Pat Rogers, AdaCore</dc:creator>
				<category><![CDATA[Ada / Ada 2005 / Ada 2012]]></category>
		<category><![CDATA[Development Log]]></category>
		<category><![CDATA[Devt log - Gem of the Week]]></category>

		<guid isPermaLink="false">http://www2.adacore.com/?p=13550</guid>
		<description><![CDATA[This series of Gems explores the GNAT Ada programming environment for the Lego Mindstorms robotics kit. The series examines the high- and low-level interfaces to the hardware, the language subset supported by the underlying run-time library, and how to use the environment effectively.  We’ll examine other topics as well.  This Gem introduces the basic steps of the Mindstorms hardware initialization and shutdown.</p>]]></description>
			<content:encoded><![CDATA[<b>Let’s get started&#8230;</b>

<p>The Mindstorms NXT “brick” contains both a 32-bit ARM processor and an 8-bit AVR processor.  The ARM executes the application code, while the AVR is responsible for the lower-level device functionality, including controlling outputs, gathering sensor inputs, and even shutting down the brick itself.</p>

<p>The two processors coordinate by sending an unending stream of messages to each other. Upon initialization, the AVR begins sending messages containing information such as the current buttons pressed, if any, and the battery status, among other data.  The ARM sends messages back to the AVR, for example to set power levels on outputs for motors or to command an analog-to-digital conversion to take place.  The messages go back and forth continually at a fixed rate.  In other words, they are not event-driven, although their content certainly reflects external events and internally generated commands.  Look in the “drivers” directory for the package body of NXT.AVR if you want to see how these messages are sent and received.  The one task declared in the entire API is located in that package body for that purpose.</p>

<p>The timing constraints on message handling are fixed by the hardware and are very strict.  It is therefore possible for messages to be lost or garbled occasionally.  A checksum is calculated to detect these errors, resulting in the problematic message being discarded.</p>

<p>The Ada API hides all these details behind high-level abstract data types for the sensors and motors, and procedural interfaces for the other lower-level facilities such as the A/D converter and discrete I/O capabilities of the ports.   Nonetheless, application code must be written with some of the above architecture in mind.  Specifically, incoming data are not available until the AVR has been initialized and has sent at least one message to the ARM.  Although the AVR is initialized automatically by the Ada drivers, the application programmer must still await receipt of that message.  For example, the current voltage of the battery is stored in a variable declared in the NXT.AVR package.  The value of that variable is dependent upon arrival of a message from the AVR and is undefined beforehand.  The accessor functions that decode the voltage representation simply process the variable as if the value is already defined. (There are ways to mitigate use of the undefined value, of course, but none are ideal.) Other values, such as the raw button readings, are also stored as variables that are set by the decoded AVR messages.</p>

<p>Therefore, the NXT.AVR package provides a procedure that awaits AVR initialization and initial message receipt.  That procedure is named Await_Data_Available. A call to this procedure should typically be the first thing done by the application.</p>

<p>Another procedure is provided to power down the brick for those applications that are intended to complete (unlike, say, embedded control systems that only stop when external power is removed).  This is procedure Power_Down, also found in the NXT.AVR package declaration. The AVR is responsible for actually shutting down the power, so the procedure injects an appropriate message to the AVR into the message stream.  However, as mentioned, messages can be lost or garbled, so the power-down request can be lost.  As a result, it is best to use an infinite loop that calls Power_Down repeatedly.  In effect, the loop “exits” when the AVR disconnects power to the brick.  For example:</p>

<pre>
   <b>loop</b>
      NXT.AVR.Power_Down;
      <b>delay</b> <b>until</b> Clock + Seconds (1);
   <b>end</b> <b>loop</b>;
</pre>

<p>The absolute delay statement emulates a relative delay (as the Ravenscar subset doesn&#8217;t include relative delays) by calling the Ada.Real_Time.Clock function and adding an interval to it.  The duration of the interval is arbitrary and need not be one second.</p>

<p>Note that the NXT.AVR package also detects the situation in which the Power button on the brick is held down for an interval, and will shut down the brick automatically in response.</p>

<p>In summary, although the API goes to some lengths to hide the gory details of the ARM/AVR interactions, ramifications
of the message-based architecture remain visible.</p>

<p>In the next Gem in this series we will explore the high-level abstract data types representing the sensors and motors.</p>
]]></content:encoded>
			<wfw:commentRss>http://www2.adacore.com/2011/12/05/gem-115-lego-mindstorms-ada-environment-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ada Connection 2011 &#8211; Debugging Mechatronic Applications Written in Ada</title>
		<link>http://www2.adacore.com/2011/11/29/debugging-mechatronic/</link>
		<comments>http://www2.adacore.com/2011/11/29/debugging-mechatronic/#comments</comments>
		<pubDate>Tue, 29 Nov 2011 16:51:49 +0000</pubDate>
		<dc:creator>AdaCore</dc:creator>
				<category><![CDATA[Ada / Ada 2005 / Ada 2012]]></category>
		<category><![CDATA[Development Log]]></category>

		<guid isPermaLink="false">http://www2.adacore.com/?p=13545</guid>
		<description><![CDATA[Here&#8217;s this Monday&#8217;s installment from the Ada Connection 2011 talks. Wiljan Derks from NXP gives a talk on Debugging Mechatronic Applications Written in Ada 




A new film added every Monday. To view all the films we&#8217;ve added to date, please visit the Ada Lecture Series. 
]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s this Monday&#8217;s installment from the <a href="http://conferences.ncl.ac.uk/adaconnection2011/" target="_blank">Ada Connection 2011</a> talks. Wiljan Derks from NXP gives a talk on Debugging Mechatronic Applications Written in Ada </p>
<br/>

<iframe width="480" height="274" src="http://www.youtube.com/embed/zqpoDVQsi0g" frameborder="0" allowfullscreen></iframe>

<p>A new film added every Monday. To view all the films we&#8217;ve added to date, please<a href="http://www.adacore.com/home/ada_answers/lectures/ada-connection-2011/"> visit the Ada Lecture Series</a>. </p>
]]></content:encoded>
			<wfw:commentRss>http://www2.adacore.com/2011/11/29/debugging-mechatronic/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Gem #114: Logging with GNATCOLL.Traces</title>
		<link>http://www2.adacore.com/2011/11/22/gem-114-logging-with-gnatcoll-traces/</link>
		<comments>http://www2.adacore.com/2011/11/22/gem-114-logging-with-gnatcoll-traces/#comments</comments>
		<pubDate>Tue, 22 Nov 2011 11:23:32 +0000</pubDate>
		<dc:creator>Emmanuel Briot, AdaCore</dc:creator>
				<category><![CDATA[Ada / Ada 2005 / Ada 2012]]></category>
		<category><![CDATA[Development Log]]></category>
		<category><![CDATA[Devt log - Gem of the Week]]></category>

		<guid isPermaLink="false">http://www2.adacore.com/?p=13451</guid>
		<description><![CDATA[The GNAT Components Collection provides a package with facilities for logging
information to various text files. Extra information can be output for each
log, providing convenient ways to understand the behavior of an application
when a debugger is not available.</p>]]></description>
			<content:encoded><![CDATA[<b>Let&#8217;s get started &#8230;</b>

<p>When we write applications, we often add Put_Line statements to help debug the
initial version of the code. Once that code works, we remove the Put_Line and
move on to some other feature of the code. The problem is that when a bug
occurs again in that part of the code (and especially when this is reported by a
user and you can&#8217;t debug on his machine), the output would still be useful, but
it&#8217;s no longer present in the executable.</p>

<p>The solution, of course, is to output the traces to some &#8220;log&#8221; file, and keep
the traces forever in the code. The log file will eventually become very large
if you have lots of traces, and finding information there might not be so
easy. So, a better solution is to split the traces into various groups, and have
a capability to display only some of the groups, so as to limit the amount of
information to look at.</p>

<p>The GNAT Components Collection includes a package GNATCOLL.Traces that
provides support for this. This package is used in various AdaCore products, in
particular GPS.</p>

<p>The first thing to do is to initialize the module. The traces are configured
via an external file, which by default is called &#8220;.gnatdebug&#8221;, and is searched
for in the current directory.</p>

<pre>
   <b>with</b> GNATCOLL.Traces;   <b>use</b> GNATCOLL.Traces;
   <b>with</b> User;
   <b>procedure</b> Main <b>is</b>
   <b>begin</b>
      Parse_Config_File;   <EM>&#45;&#45;  parses default ./.gnatdebug</EM>
      User.Proc;
   <b>end</b> Main;
</pre>

<p>The simplest way to compile this code is to use a project file. For instance:</p>

<pre>
<b>with</b> &quot;gnatcoll&quot;;
<b>project</b> Default <b>is</b>
    <b>for</b> Main <b>use</b> (&quot;main.adb&quot;);
<b>end</b> Default;

gprbuild -Pdefault.gpr
</pre>

<p>In the rest of the code we can create a stream. All log messages are sent to a
stream, and you are free to create as many as you want. A log message will
always be prefixed by the name of its stream, as a way to organize traces in
the log file. Here is what the Ada code would look like:</p>

<pre>
<b>package</b> User <b>is</b>
   Stream1 : <b>constant</b> Trace_Handle := Create (&quot;Stream1&quot;);

   <b>procedure</b> Proc <b>is</b>
   <b>begin</b>
      Trace (Stream1, &quot;Some trace&quot;);
   <b>end</b> Proc;
<b>end</b> User;
</pre>

<p>If you compile this code and run it, there will in fact be nothing
logged. That&#8217;s because the trace streams are disabled by default. We thus need
to create a configuration file. Its format is very simple. The following
example demonstrates a number of its features:</p>

<pre>
+
&gt; log
TRACE1=yes
TRACE2=no
TRACE3=yes &gt; log2
DEBUG.COLORS=yes
</pre>

<p>The first line (&#8220;+&#8221;) indicates that we would like to activate all the streams
by default. So a file with only that line would already show the output in our
Ada example.</p>

<p>The second line (&#8220;>log&#8221;) indicates where the output of the streams should go by
default. If this isn&#8217;t specified, the output goes to stdout. Otherwise, you can
specify a file name. Advanced usage allow you to define other types of
redirection. For instance, GNATCOLL comes by default with support for
redirecting to syslog on Unix systems. It would be relatively simple to add
support for custom outputs, like a socket, a database, etc.</p>

<p>The next three lines configure specific streams and show how to activate or
disable them. The fifth line, in particular, redirects one of the streams to
another log file.</p>

<p>The last line in the example activates one of the extra features of
GNATCOLL.Traces. Activating &#8220;DEBUG.COLORS&#8221; will output the stream using
different colors for the various information that is output on each line.</p>

<p>Other facilities are available. For instance, if you activate
&#8220;DEBUG.ABSOLUTE_TIME&#8221;, each line in the log file will include the time
of the message. More powerfully, you can activate &#8220;DEBUG.STACK_TRACE&#8221; to get a
stack trace for each message (note that the trace needs to be converted via addr2line).
Another useful one is &#8220;DEBUG.COUNT&#8221;, which will add the number of messages
output so far in total and for the specific stream.</p>

<p>Here is what the log file will look like:</p>

<pre>
[STREAM1] 1/1 Some Trace (09:05:32.323)
[STREAM4] 1/2 Some Other Trace (09:05:33.125)
</pre>

<p>As we have seen in the example, text is output using the Trace function. There
are two versions of it: the one we saw that is used to output a simple
message, and another version that takes an exception occurrence as a parameter
and outputs information about that exception. There is also a procedure called
Assert that will output an error message if a Condition is False. The error
will be displayed in red if the colors were activated, thus making it easy to
locate in the log file.</p>

<p>Preparing the text for a message might be expensive (for instance, if the text
should contain the result of a function call, or requires a lot of string
concatenation). For this, the recommended coding pattern is:</p>

<pre>
   <b>if</b> Active (Stream1) <b>then</b>
      Trace (Stream1, &quot;Function result was &quot; &amp; Func(...));
   <b>end</b> <b>if</b>;
</pre>

<p>The log file might still be hard to read when it gets big. GNATCOLL.Traces
provides a facility for indenting the traces. Here is a full example of code
computing Fibonacci numbers recursively:</p>

<pre>
<b>pragma</b> Ada_05;
<b>with</b> Ada.Text_IO;       <b>use</b> Ada.Text_IO;
<b>with</b> GNATCOLL.Traces;   <b>use</b> GNATCOLL.Traces;

<b>procedure</b> Fibo <b>is</b>
   Me : <b>constant</b> Trace_Handle := Create (&quot;FIBO&quot;);

   <b>function</b> Recurse (Num : Positive) <b>return</b> Positive <b>is</b>
      Result : Integer;
   <b>begin</b>
      <b>if</b> Num = 1 <b>or</b> <b>else</b> Num = 2 <b>then</b>
         <b>return</b> 1;
      <b>end</b> <b>if</b>;

      Increase_Indent (Me, &quot;Computing&quot; &amp; Num'Img);
      Result := Recurse (Num - 1) + Recurse (Num - 2);
      Decrease_Indent (Me, &quot;Done =&gt;&quot; &amp; Result'Img);
      <b>return</b> Result;
   <b>end</b> Recurse;

<b>begin</b>
   Parse_Config_File;
   Put_Line (Recurse (5)'Img);
<b>end</b> Fibo;
</pre>

<p>The code should be compiled with a project file, as shown earlier. The current
directory should also contain a file called &#8220;.gnatdebug&#8221; with a single line
that contains &#8220;+&#8221;. The output on stdout is:</p>

<pre>
[FIBO] 1/1 Computing 5
   [FIBO] 2/2 Computing 4
      [FIBO] 3/3 Computing 3
      [FIBO] 4/4 Done =&gt; 2
   [FIBO] 5/5 Done =&gt; 3
   [FIBO] 6/6 Computing 3
   [FIBO] 7/7 Done =&gt; 2
[FIBO] 8/8 Done =&gt; 5
 5
</pre>

<p>which shows the indentation that is increased for each recursive call.</p>

<p>GNATCOLL.Traces uses object-oriented code. It provides a number of hooks
through which you can add your own extra data each time some line is output to
the log file. For this, you should override the Pre_Decorator or Post_Decorator
primitive operations.
</p>
]]></content:encoded>
			<wfw:commentRss>http://www2.adacore.com/2011/11/22/gem-114-logging-with-gnatcoll-traces/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ada Connection 2011 &#8211; A Real-Time Framework for Multiprocessor Platforms Using Ada 2012</title>
		<link>http://www2.adacore.com/2011/11/21/real-time-framework-for-multiprocessor-platforms-using-ada-2012/</link>
		<comments>http://www2.adacore.com/2011/11/21/real-time-framework-for-multiprocessor-platforms-using-ada-2012/#comments</comments>
		<pubDate>Mon, 21 Nov 2011 14:23:26 +0000</pubDate>
		<dc:creator>AdaCore</dc:creator>
				<category><![CDATA[Ada / Ada 2005 / Ada 2012]]></category>
		<category><![CDATA[Development Log]]></category>

		<guid isPermaLink="false">http://www2.adacore.com/?p=13516</guid>
		<description><![CDATA[Here&#8217;s this Monday&#8217;s installment from the Ada Connection 2011 talks. Sergio Saez from Universidad Politecnica de Valencia gives a talk on A Real-Time Framework for Multiprocessor Platforms Using Ada 2012




A new film added every Monday. To view all the films we&#8217;ve added to date, please visit the Ada Lecture Series. 
]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s this Monday&#8217;s installment from the <a href="http://conferences.ncl.ac.uk/adaconnection2011/" target="_blank">Ada Connection 2011</a> talks. Sergio Saez from Universidad Politecnica de Valencia gives a talk on A Real-Time Framework for Multiprocessor Platforms Using Ada 2012</p>
<br/>

<iframe width="480" height="274" src="http://www.youtube.com/embed/dxon9Ll3Ugo" frameborder="0" allowfullscreen></iframe>

<p>A new film added every Monday. To view all the films we&#8217;ve added to date, please<a href="http://www.adacore.com/home/ada_answers/lectures/ada-connection-2011/"> visit the Ada Lecture Series</a>. </p>
]]></content:encoded>
			<wfw:commentRss>http://www2.adacore.com/2011/11/21/real-time-framework-for-multiprocessor-platforms-using-ada-2012/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>High-Level Synthesis with GAUT</title>
		<link>http://www2.adacore.com/2011/11/18/gaut/</link>
		<comments>http://www2.adacore.com/2011/11/18/gaut/#comments</comments>
		<pubDate>Fri, 18 Nov 2011 09:24:51 +0000</pubDate>
		<dc:creator>AdaCore</dc:creator>
				<category><![CDATA[Ada / Ada 2005 / Ada 2012]]></category>
		<category><![CDATA[Development Log]]></category>

		<guid isPermaLink="false">http://www2.adacore.com/?p=13486</guid>
		<description><![CDATA[
In a recent micro seminar,  Dr. Philippe Coussy Associate Professor at the Université de Bretagne-Sud&#8217;s Science and Technique Department gave a talk on high-level synthesis principles and tools:



High-Level Synthesis with GAUT
]]></description>
			<content:encoded><![CDATA[
<p>In a recent micro seminar,  <a href="http://www-labsticc.univ-ubs.fr/~coussy/cv-coussy.pdf">Dr. Philippe Coussy</a> Associate Professor at the Université de Bretagne-Sud&#8217;s Science and Technique Department gave a talk on high-level synthesis principles and tools:<br/>
<br/>


<a href="http://www.slideshare.net/AdaCore/highlevel-synthesis-with-gaut">High-Level Synthesis with GAUT</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www2.adacore.com/2011/11/18/gaut/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ParaSail language &#8211; AdaCore micro seminar</title>
		<link>http://www2.adacore.com/2011/11/14/parasail-seminar/</link>
		<comments>http://www2.adacore.com/2011/11/14/parasail-seminar/#comments</comments>
		<pubDate>Mon, 14 Nov 2011 16:56:18 +0000</pubDate>
		<dc:creator>AdaCore</dc:creator>
				<category><![CDATA[Ada / Ada 2005 / Ada 2012]]></category>
		<category><![CDATA[Development Log]]></category>

		<guid isPermaLink="false">http://www2.adacore.com/?p=13466</guid>
		<description><![CDATA[AdaCore Internal Seminar &#8211; December 2, 2011

ParaSail &#8211; A new programming language designed to allow productive development of parallell, high-integrity (safety and security-critical) software systems. The language is tentatively named &#8220;ParaSail&#8221; for parallel, specification and implementation language. For more info, please visit:

http://parasail-programming-language.blogspot.com/

Presenter: Tucker Taft.

UPDATE: View slides from the seminar.

 ParaSail    View more [...]]]></description>
			<content:encoded><![CDATA[<p><em>AdaCore Internal Seminar &#8211; December 2, 2011</em><br/><br/>

ParaSail &#8211; A new programming language designed to allow productive development of parallell, high-integrity (safety and security-critical) software systems. The language is tentatively named &#8220;ParaSail&#8221; for parallel, specification and implementation language. For more info, please visit:</br></br>

<a href="http://parasail-programming-language.blogspot.com/">http://parasail-programming-language.blogspot.com/</a>
<br/><br/>
Presenter: <a href="http://www.sofcheck.com/company/taft_bio.html">Tucker Taft</a>.</p>

<p>UPDATE: <em>View slides from the seminar.</em></p>

<div style="width:425px" id="__ss_10573983"> <strong style="display:block;margin:12px 0 4px"><a href="http://www.slideshare.net/AdaCore/parasail" title="ParaSail " target="_blank">ParaSail </a></strong> <iframe src="http://www.slideshare.net/slideshow/embed_code/10573983" width="425" height="355" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe> <div style="padding:5px 0 12px"> View more <a href="http://www.slideshare.net/" target="_blank">presentations</a> from <a href="http://www.slideshare.net/AdaCore" target="_blank">AdaCore</a> </div> </div>

<p>AdaCore, from time to time, organizes seminars in the Paris/NY offices. If you are interested in a particular talk, please send email to <a href="mailto: events@adacore.com">events@adacore.com</a>.</p>

]]></content:encoded>
			<wfw:commentRss>http://www2.adacore.com/2011/11/14/parasail-seminar/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ada Connection 2011 &#8211; Real-Time Management &amp; Production Systems for Manufacturing and Energy Facilities</title>
		<link>http://www2.adacore.com/2011/11/14/ada-connection-2011-systems-for-manufacturing-and-energy/</link>
		<comments>http://www2.adacore.com/2011/11/14/ada-connection-2011-systems-for-manufacturing-and-energy/#comments</comments>
		<pubDate>Mon, 14 Nov 2011 14:48:53 +0000</pubDate>
		<dc:creator>AdaCore</dc:creator>
				<category><![CDATA[Ada / Ada 2005 / Ada 2012]]></category>
		<category><![CDATA[Development Log]]></category>

		<guid isPermaLink="false">http://www2.adacore.com/?p=13464</guid>
		<description><![CDATA[Here&#8217;s this Monday&#8217;s installment from the Ada Connection 2011 talks. Jozef Cvirik from Ipesoft gives a talk on Real-Time Management &#038; Production Systems for Manufacturing and Energy Facilities




A new film added every Monday. To view all the films we&#8217;ve added to date, please visit the Ada Lecture Series. 
]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s this Monday&#8217;s installment from the <a href="http://conferences.ncl.ac.uk/adaconnection2011/" target="_blank">Ada Connection 2011</a> talks. Jozef Cvirik from Ipesoft gives a talk on Real-Time Management &#038; Production Systems for Manufacturing and Energy Facilities</p>
<br/>

<iframe width="480" height="274" src="http://www.youtube.com/embed/zOXhRFSFCd0" frameborder="0" allowfullscreen></iframe>

<p>A new film added every Monday. To view all the films we&#8217;ve added to date, please<a href="http://www.adacore.com/home/ada_answers/lectures/ada-connection-2011/"> visit the Ada Lecture Series</a>. </p>
]]></content:encoded>
			<wfw:commentRss>http://www2.adacore.com/2011/11/14/ada-connection-2011-systems-for-manufacturing-and-energy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

